在android中使用SQLiteDataBase时显示空数据库

时间:2016-03-08 08:30:45

标签: android listview android-database

我想在我的应用程序中使用DataBase。我使用ListView显示数据库项目,点击ListView项目,在其他活动中显示详细信息!点击ListView项目以显示详细信息,startActivity向我显示此错误:

03-08 11:48:01.501 13623-13623/com.tellfa.dastanak E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.tellfa.dastanak, PID: 13623
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tellfa.dastanak/com.tellfa.dastanak.Activities.StoryDetail_Page}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.tellfa.dastanak.Database.DataBase.getReadableDatabase()' on a null object reference
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:135)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5349)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:372)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
                                                                      Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase com.tellfa.dastanak.Database.DataBase.getReadableDatabase()' on a null object reference
                                                                         at com.tellfa.dastanak.Activities.StoryDetail_Page.onCreate(StoryDetail_Page.java:39)
                                                                         at android.app.Activity.performCreate(Activity.java:6020)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2284)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391) 
                                                                         at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1309) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:135) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5349) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703) 

第二个活动代码:

public class StoryDetail_Page extends AppCompatActivity {

    FloatingActionButton story_fab_favorite;
    ImageView story_image_header;
    TextView story_text_description;
    SQLiteDatabase sqLiteDatabase;
    DataBase dataBase;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.story_detail_page);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        story_fab_favorite = (FloatingActionButton) findViewById(R.id.story_favorite_fab);
        story_image_header = (ImageView) findViewById(R.id.story_header_image);
        story_text_description = (TextView) findViewById(R.id.story_content_text);

        final DataProvider dataProvider = (DataProvider) getIntent().getSerializableExtra("share_data");
        sqLiteDatabase = dataBase.getReadableDatabase();

        story_text_description.setText(dataProvider.getKey_description());

        sqLiteDatabase.close();
    }

ListView代码(片段中):

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        dataProvider = (DataProvider) cat1FragAdapter.getItem(position);
        Bundle bundle = new Bundle();
        bundle.putSerializable("share_data", dataProvider);
        Intent intent = new Intent(getActivity(), StoryDetail_Page.class);
        intent.putExtras(bundle);
        startActivity(intent);
    }
});

我怎样才能适应这个问题? tnx all&lt; 3

0 个答案:

没有答案