在AppView中调用数据时,Android应用程序停止

时间:2014-06-03 15:57:44

标签: java android listview

我正在尝试在ListView中显示数据,但我的应用程序停止运行。我在其他活动类中有一个方法,方法:public List <Presentation> getAllPresentations() {..}我希望在ListView中查看此数据,如下所示:

public class view_presentations extends Activity
{
    DatabaseHelper db;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_presentation);

        List<Presentation> list = db.getAllPresentations();
        ListView l = (ListView)findViewById(R.id.list);
        l.setAdapter(new ArrayAdapter<Presentation>(view_presentations.this, R.layout.view_presentation, list));

        db.closeDB();
    }
}

这是LogCat中的输出:

06-03 17:46:22.123: W/dalvikvm(27677): threadid=1: thread exiting with uncaught exception (group=0x40adf228)
06-03 17:46:22.163: E/AndroidRuntime(27677): FATAL EXCEPTION: main
06-03 17:46:22.163: E/AndroidRuntime(27677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ricordiemotivi/com.example.ricordiemotivi.view_presentations}: java.lang.NullPointerException
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.os.Looper.loop(Looper.java:156)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread.main(ActivityThread.java:4987)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at java.lang.reflect.Method.invoke(Method.java:511)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at dalvik.system.NativeStart.main(Native Method)
06-03 17:46:22.163: E/AndroidRuntime(27677): Caused by: java.lang.NullPointerException
06-03 17:46:22.163: E/AndroidRuntime(27677):    at com.example.ricordiemotivi.view_presentations.onCreate(view_presentations.java:24)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.Activity.performCreate(Activity.java:4538)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
06-03 17:46:22.163: E/AndroidRuntime(27677):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
06-03 17:46:22.163: E/AndroidRuntime(27677):    ... 11 more

我测试了数据库输出,一切正常 有人能帮助我吗?

2 个答案:

答案 0 :(得分:4)

您尚未初始化db。当您致电db.getAllPresentations()时,您会NullPointerException因为db为空。在onCreate

db = new DatabaseHelper(this);

答案 1 :(得分:0)

DatabaseHelper db = new DatabaseHelper(...);