无法在微调器上显示数据?抛出空指针异常
谁能告诉我这段代码中有什么错误吗...我把代码放在下面。
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)
请有人帮助我。
提前致谢
can anyone tell wats wrong in this code... I put code bellow.
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 class having following code
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;
Null pointer Exception. How to avoid it.
This is the error on 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)
Pls someone help me.
Thanks in advance
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它看起来像这一行:
sqLiteDatabase
为 null - 确保它在您的onCreate
方法中初始化。It looks like in this line:
that
sqLiteDatabase
is null - make sure it is initialised in youronCreate
method.为什么不能在 displayCategory() 方法中放置断点?空指针异常很容易找到。正如 Cjk 所指出的,sqlitedatabase 是统一的。调试将帮助您在浪费大量时间之前发现并解决问题。
why cant you put a breakpoint in the displayCategory() method? null pointer exceptions are quite easy to find.As Cjk has pointed out, sqlitedatabase is unitialised. Debugging will help you find and solve issues before you lose much of your time..