Android SQLite导致崩溃

时间:2014-03-05 14:36:51

标签: android sqlite crash

每当调用此方法时,我的应用程序都会立即崩溃,而且我已经搞乱了几个小时了。我对SQL很新,甚至更新的Android编程,所以有什么东西可以直接跳出来,因为我的代码错了吗?我想我可能会错过与光标有关的其他东西,但我没有人要问。提前致谢:

public String fillTimetable(String day, String week, String lesson){
    String[] column = new String[]{KEY_SUBJECT};
    String selection = "WHERE "+KEY_DAY+" = '"+day+"' AND "+KEY_WEEK+" = '"+week+"' AND "+KEY_LESSON+" = '"+lesson+"';";
    Cursor c = ourDatabase.query(DATABASE_TABLE, column, selection, null, null,
            null, null);

    int iSubject = c.getColumnIndex(KEY_SUBJECT);
    return c.getString(iSubject);

}//end fillTimetable

Log Cat报告:

02-27 22:17:52.180: E/AndroidRuntime(8062): FATAL EXCEPTION: main
02-27 22:17:52.180: E/AndroidRuntime(8062): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.shearer.schooltimetable/com.shearer.schooltimetable.Tabs}: java.lang.NullPointerException
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2106)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.os.Looper.loop(Looper.java:137)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread.main(ActivityThread.java:5041)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at java.lang.reflect.Method.invokeNative(Native Method)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at java.lang.reflect.Method.invoke(Method.java:511)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at dalvik.system.NativeStart.main(Native Method)
02-27 22:17:52.180: E/AndroidRuntime(8062): Caused by: java.lang.NullPointerException
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.Activity.findViewById(Activity.java:1839)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at com.shearer.schooltimetable.Tabs.<init>(Tabs.java:19)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at java.lang.Class.newInstanceImpl(Native Method)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at java.lang.Class.newInstance(Class.java:1319)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.Instrumentation.newActivity(Instrumentation.java:1054)
02-27 22:17:52.180: E/AndroidRuntime(8062):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
02-27 22:17:52.180: E/AndroidRuntime(8062):     ... 11 more

1 个答案:

答案 0 :(得分:1)

来自stacktrace:

Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1839)
at com.shearer.schooltimetable.Tabs.<init>(Tabs.java:19)

您在findViewById()之前致电setContentView()。在onCreate()之后将调用从成员变量初始化移动到活动setContentView()

根据您发布的代码,selection应省略WHERE

String selection = KEY_DAY+" = '"+day+"' AND "+KEY_WEEK+" = '"+week+"' AND "+KEY_LESSON+" = '"+lesson+"';";

?占位符用于selectionArgs的文字也是明智的。避免SQL注入,例如:

String selection = KEY_DAY+" = ? AND "+KEY_WEEK+" = ? AND "+KEY_LESSON+" = ?";
String[] selectionArgs = new String[] { day, week, lesson };
Cursor c = ...query(..., selection, selectionArgs, ...);

query()后,Cursor尚未指向有效的结果行。在尝试使用moveTo...访问行数据之前,使用其中一种getString()方法移动它:

Cursor c = ....;
String result = null;
if (c.moveToFirst()) {
    int iSubject = c.getColumnIndex(KEY_SUBJECT);
    result = c.getString(iSubject);
}
c.close();
return result;

如上所示,明确关闭游标也是一个好主意。