我在Google Play Console中收到了此崩溃报告,但我无法重现:
java.lang.RuntimeException:
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2422)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2482)
at android.app.ActivityThread.access$900 (ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1344)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5423)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
Caused by: android.database.sqlite.SQLiteException:
at android.database.sqlite.SQLiteConnection.nativePrepareStatement (SQLiteConnection.java)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement (SQLiteConnection.java:887)
at android.database.sqlite.SQLiteConnection.prepare (SQLiteConnection.java:498)
at android.database.sqlite.SQLiteSession.prepare (SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init> (SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init> (SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query (SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory (SQLiteDatabase.java:1316)
at android.database.sqlite.SQLiteDatabase.rawQuery (SQLiteDatabase.java:1255)
at .StatDB.getExercise (StatDB.java)
at .ExerciseList.onCreate (ExerciseList.java)
at android.app.Activity.performCreate (Activity.java:6294)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2375)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2482)
at android.app.ActivityThread.access$900 (ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1344)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:148)
at android.app.ActivityThread.main (ActivityThread.java:5423)
at java.lang.reflect.Method.invoke (Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:616)
这是我的getExercise
方法:
public List<Bean> getExercise(String sub) {
SQLiteDatabase db = initializer.getReadableDatabase();
String sql = "SELECT * FROM " + sub;
Cursor cursor = db.rawQuery(sql, null);
List<Bean> exeList = new ArrayList<Bean>();
while(cursor.moveToNext()) {
String code = cursor.getString(1);
String mark = cursor.getString(2);
exeList.add(new Bean(code, mark));
}
cursor.close();
db.close();
return exeList;
}
在我的ExerciseList.onCreate
:
Intent i = getIntent();
subCode = i.getExtras().getString("sub");
List<Bean> exeList = statDB.getExercise(subCode);
我之前曾提到“崩溃可能因为subCode
是空的吗?但是在我进行测试后,我排除了这个原因。因为在List<Bean> exeList = statDB.getExercise(subCode);
之前,实际上我用这个设置我的TextView
:
if (subCode.equals("bd")) {
passTv.setText("Some Text");
} else {
passTv.setText("Another Text");
}
所以更完整的代码是:
Intent i = getIntent();
subCode = i.getExtras().getString("sub");
if (subCode.equals("bd")) {
passTv.setText("Some Text");
} else {
passTv.setText("Another Text");
}
List<Bean> exeList = statDB.getExercise(subCode);
如果我的subcode
为空,我的TextView
将首先崩溃。所以问题是可疑的是我的getExercise
方法,特别是String sql = "SELECT * FROM " + sub;
。是因为参数sub
没有通过?
根据您的问题:
崩溃是否因为subCode为空而发生?
如果你不知道如果sub
是null
会发生什么:
String sql = "SELECT * FROM " + sub;
好吧,检查很简单:
String sub = null;
String sql = "SELECT * FROM " + sub;
System.out.println(sql);
SELECT * FROM null
只需检查null
值以防止发送错误的查询。
if(sub != null){
throw new Exception("no table provided")
}
String sql = "SELECT * FROM " + sub;
这可能不是创建这样的查询的最佳方法。 首先,请参阅SQL注入。 那么,当你的表没有2个TEXT字段时会发生什么?您应该使用DAO模式为每个表创建一个类,而不是希望最好。