,所以我得到這個錯誤,我真的不知道爲什麼,我已經檢查清單了java.lang.RuntimeException:無法啓動活動ComponentInfo ... NullPointerException異常
01-17 11:17:30.190: E/AndroidRuntime(2225): FATAL EXCEPTION: main
01-17 11:17:30.190: E/AndroidRuntime(2225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.examaar/com.example.examaar.Final}: java.lang.NullPointerException
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.os.Handler.dispatchMessage(Handler.java:99)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.os.Looper.loop(Looper.java:130)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-17 11:17:30.190: E/AndroidRuntime(2225): at java.lang.reflect.Method.invokeNative(Native Method)
01-17 11:17:30.190: E/AndroidRuntime(2225): at java.lang.reflect.Method.invoke(Method.java:507)
01-17 11:17:30.190: E/AndroidRuntime(2225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:880)
01-17 11:17:30.190: E/AndroidRuntime(2225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:638)
01-17 11:17:30.190: E/AndroidRuntime(2225): at dalvik.system.NativeStart.main(Native Method)
01-17 11:17:30.190: E/AndroidRuntime(2225): Caused by: java.lang.NullPointerException
01-17 11:17:30.190: E/AndroidRuntime(2225): at com.example.examaar.DBHelper.selectAllBooks(DBHelper.java:85)
01-17 11:17:30.190: E/AndroidRuntime(2225): at com.example.examaar.Final.onCreate(Final.java:17)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-17 11:17:30.190: E/AndroidRuntime(2225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
01-17 11:17:30.190: E/AndroidRuntime(2225): ... 11 more
,就是這樣最後一個活動的代碼,你正在使用lol[i]
(觸發此異常的一個)
package com.example.examaar;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class Final extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.final1);
ListView lv = (ListView) findViewById(R.id.listView1);
final DBHelper db = new DBHelper(this);
ArrayList<Libro> libros = db.selectAllBooks();
String[] res= new String[libros.size()];
for(int i=0;i<libros.size();i++){
res[i]=libros.get(i).getusuario()+"-"+libros.get(i).getdestino()+"-"+libros.get(i).getfecha()+"-"+libros.get(i).gettransporte();
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1, res);
lv.setAdapter(arrayAdapter);
}
}
繼續往下看堆棧跟蹤,它顯示''NullPointerException'在'DBHelper.selectAllBooks'的第85行。我建議在那裏尋找你的問題,如果你仍然卡住,在這裏發佈你的DBHelper代碼。 – Yjay
@Yjay是啊我嘗試過,但仍然沒有運氣,在第二項活動中,我在上次活動中實施了與res [i]相同值的日誌,並且它給了我一個結果。 – user3207414
好的我發現什麼觸發了異常,它是selectAllBooks,但它沒有錯,我只是意識到,在我上次的活動中,沒有創建與我的數據庫連接。哇,羅基錯誤! – user3207414