java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
代码是这样的:
Cursor cursor = null;
if (inputText == null || inputText.length() == 0) {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE }, GL_FK + " like '%" + inputText
+ "%'", null, null, null, null, null);
} else {
cursor = myDataBase.query(true, GL_TABLE, new String[] { GL_ID,
GL_FK, GL_LANG, GL_VALUE }, GL_VALUE + " like '%"
+ inputText + "%'", null, null, null, null, null);
}
if (cursor != null) {
cursor.moveToFirst();
}
String[] columns = new String[] { GL_FK, GL_LANG, GL_VALUE };
int[] to = new int[] { R.id.tvWord, R.id.tvMeaning, R.id.tvKanji };
dataAdapter = new SimpleCursorAdapter(mContext, R.layout.listword,
cursor, columns, to, 0);
MainActivity mainActivity = new MainActivity();
ListView listView = mainActivity.wordlist;
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the
// result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String countryCode = cursor.getString(cursor
.getColumnIndexOrThrow("code"));
Toast.makeText(mContext, countryCode, Toast.LENGTH_SHORT)
.show();
}
});
我遵循了tutorial。它从SQLite数据库获取数据并放入listview。当我试图测试它时,它给出了错误:java.lang.RuntimeException:无法实例化活动。不知道是什么原因导致这个问题。请提出一些如何解决这个问题的建议。
完整的堆栈跟踪:
08-05 21:36:01.375: W/dalvikvm(2367): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-05 21:36:01.382: E/AndroidRuntime(2367): FATAL EXCEPTION: main
08-05 21:36:01.382: E/AndroidRuntime(2367): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.os.Looper.loop(Looper.java:130)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 21:36:01.382: E/AndroidRuntime(2367): at dalvik.system.NativeStart.main(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): Caused by: java.lang.NullPointerException
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.Activity.findViewById(Activity.java:1647)
08-05 21:36:01.382: E/AndroidRuntime(2367): at com.joericharduz.akita.MainActivity.<init>(MainActivity.java:20)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.Class.newInstanceImpl(Native Method)
08-05 21:36:01.382: E/AndroidRuntime(2367): at java.lang.Class.newInstance(Class.java:1409)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 21:36:01.382: E/AndroidRuntime(2367): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-05 21:36:01.382: E/AndroidRuntime(2367): ... 11 more
08-05 21:37:13.851: W/dalvikvm(2579): threadid=1: thread exiting with uncaught exception (group=0x40018578)
08-05 21:37:13.937: E/AndroidRuntime(2579): FATAL EXCEPTION: main
08-05 21:37:13.937: E/AndroidRuntime(2579): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1573)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.os.Handler.dispatchMessage(Handler.java:99)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.os.Looper.loop(Looper.java:130)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.main(ActivityThread.java:3687)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.reflect.Method.invokeNative(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.reflect.Method.invoke(Method.java:507)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
08-05 21:37:13.937: E/AndroidRuntime(2579): at dalvik.system.NativeStart.main(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): Caused by: java.lang.NullPointerException
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.Activity.findViewById(Activity.java:1647)
08-05 21:37:13.937: E/AndroidRuntime(2579): at com.joericharduz.akita.MainActivity.<init>(MainActivity.java:20)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.Class.newInstanceImpl(Native Method)
08-05 21:37:13.937: E/AndroidRuntime(2579): at java.lang.Class.newInstance(Class.java:1409)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
08-05 21:37:13.937: E/AndroidRuntime(2579): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1565)
08-05 21:37:13.937: E/AndroidRuntime(2579): ... 11 more
答案 0 :(得分:2)
在第20行检查MainActivity.java
。由于在调用findViewById时发生了NPE(NullPointerException),这可能是因为您在NULL活动上调用了findViewById,或者在具有NULL的Activity上调用了window(AOSP中findViewById的实现只是调用getWindow()。findViewById())。
仔细检查当您的Activity处于正确状态时,您只调用findViewById()。也就是说,在尝试执行与窗口或视图有关的任何操作之前,您应该等到onCreate
回调。
另外,用new MainActivity()
之类的东西来实例化一个活动是非常奇怪的,除非你试图完成一些非常不寻常和高级的东西,否则很可能无法工作。
答案 1 :(得分:1)
空指针异常是我们能看到的最美丽的例外。找出哪条线制作这个NPE,然后回溯!它肯定你在NULL OBJECT上调用了一些东西!