任何人都可以在这段代码中告诉wats错误...我把代码放在下面。
mySQLiteAdapter = new SQLiteAdapter(this);
cursor = mySQLiteAdapter.displayCategory();
startManagingCursor(cursor);
String[] from = new String[] { SQLiteAdapter.KEY_ID, SQLiteAdapter.KEY_CATEGORY };
int[] to = new int[] { android.R.id.text1 };
mCategory = (Spinner) findViewById(R.id.choose_category);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cursor, from, to);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mCategory.setAdapter(adapter);
MyAdapter类具有以下代码
public Cursor displayCategory() {
String[] columns = new String[]{KEY_ID, KEY_CATEGORY};
Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns,
null, null, null, null, null);
return cursor;
空指针异常。如何避免它。 这是log cat
上的错误 09-09 15:49:16.535: ERROR/AndroidRuntime(6526): Uncaught handler: thread main exiting due to uncaught exception
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.mpt.receiptor/com.android.mpt.receiptor.SearchReceipt}: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.os.Handler.dispatchMessage(Handler.java:99)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.os.Looper.loop(Looper.java:123)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.main(ActivityThread.java:4363)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at java.lang.reflect.Method.invokeNative(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at java.lang.reflect.Method.invoke(Method.java:521)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at dalvik.system.NativeStart.main(Native Method)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): Caused by: java.lang.NullPointerException
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.mpt.receiptor.SQLiteAdapter.displayCategory(SQLiteAdapter.java:135)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at com.android.mpt.receiptor.SearchReceipt.onCreate(SearchReceipt.java:96)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-09 15:49:16.545: ERROR/AndroidRuntime(6526): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
请别人帮帮我。
提前致谢
答案 0 :(得分:1)
看起来就像这一行:
Cursor cursor = sqLiteDatabase.query(MYCATEGORY_TABLE, columns,
null, null, null, null, null);
sqLiteDatabase
为空 - 请确保在onCreate
方法中对其进行初始化。
答案 1 :(得分:1)
为什么你不能在displayCategory()方法中放置一个断点?空指针异常很容易找到。正如Cjk指出的那样,sqlitedatabase是单元化的。调试将帮助您在失去大量时间之前找到并解决问题..