java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.joericharduz.akita/com.joericharduz.akita.MainActivity}: java.lang.NullPointerException
代碼是這樣的:無法實例活動:顯示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數據庫獲取數據並放入列表視圖。當我試圖測試它時,它給了錯誤: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
請發佈完整的堆棧跟蹤 – Raghunandan
stacktrace可能有一些行號,這將有助於查明引用null發生的位置。 –
更好的是,看看你的logcat,然後雙擊正在接受NPE的異常。然後修復它。 – 323go