0
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 
+1

請發佈完整的堆棧跟蹤 – Raghunandan

+0

stacktrace可能有一些行號,這將有助於查明引用null發生的位置。 –

+0

更好的是,看看你的logcat,然後雙擊正在接受NPE的異常。然後修復它。 – 323go

回答

2

檢查MainActivity.java在20行由於NPE(空指針異常)正在發生的事情,當你做出findViewById打電話,這可能是因爲你在一個空的活動呼籲findViewById,或在具有NULL窗口的Activity上(AOSP中findViewById的實現只是調用getWindow()。findViewById())。

仔細檢查一下,當您的活動處於正確狀態時,您只調用findViewById()。也就是說,你應該等到你得到一個onCreate回調之後,才能嘗試做任何與窗口或視圖有關的事情。

此外,使用諸如new MainActivity()之類的東西來實例化一個活動是非常奇怪的,並且可能不起作用,除非您試圖完成非常不尋常和先進的事情。

+0

在NULL Activity上調用findViewById。這是什麼意思? –

1

空指針異常是我們可以看到的最美麗的異常。找到哪一行製作這個NPE,然後回溯!它肯定你在NULL對象上調用了一些東西!

相關問題