getWritableDatabase()处的nullPointerException

时间:2013-07-07 18:22:18

标签: java android nullpointerexception

Android新手。我已经创建了一个新的Fragments应用程序,但我无法从Fragment访问数据库。

这是班级:

public class ShopListActivity extends FragmentActivity {
    SectionsPagerAdapter mSectionsPagerAdapter;
    ViewPager mViewPager;
    DatabaseHelper db;

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

        db = new DatabaseHelper(this); // tried getApplicationContext(), and getBaseContext() as well

        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    public void onClickAddProduct(View view) {
        TextView productName = (TextView) findViewById(R.id.add_product_text);
        SQLiteDatabase dbWritable = db.getWritableDatabase(); // <-- crashes here

        ContentValues values = new ContentValues();
        values.put("Name", (String) productName.getText());

        dbWritable.insert("Lists", "Name", values);
    }

我在SO上找到的所有其他解决方案都无济于事。 DatabaseHelper是一个非常基本的DB Helper类:

public DatabaseHelper(Context context) {
    super(context, dbName, null, dbVersion);
}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(SQL_CREATE_TABLES);
    db.execSQL("INSERT INTO Lists (Name) VALUES('Test')");
}

这是Logcat:

07-07 21:03:24.351    9641-9641/com.chas.shopforme             E/AndroidRuntime: FATAL EXCEPTION: main
        java.lang.IllegalStateException: Could not execute method of the activity
        at android.view.View$1.onClick(View.java:3606)
        at android.view.View.performClick(View.java:4211)
        at android.view.View$PerformClick.run(View.java:17362)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5227)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)
        Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at android.view.View$1.onClick(View.java:3601)
        ... 11 more
        Caused by: java.lang.NullPointerException
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at com.chas.shopforme.ShopListActivity.onClickAddProduct(ShopListActivity.java:69)
        ... 14 more

注意:我知道有更多这样的问题,但他们的解决方案对我不起作用。

2 个答案:

答案 0 :(得分:2)

试试这个

db = new DatabaseHelper(this.getApplicationContext()); 

答案 1 :(得分:1)

我尝试将此添加为对您的问题的评论,但在评论中编码时会发生令人讨厌的事情。

假设你在上面的评论中尝试过a)并且它没有用,那么b)的一个例子如下:

protected void onCreate(Bundle savedInstanceState) {

...

    Handler hand = new Handler();
    hand.post(new Runnable() {
        public void run() {
            dbInit();
        }
    });
}

public void dbInit() {
    db = new DatabaseHelper(this);
}

这允许片段管理器在您尝试使用它之前完成片段的设置。它正在做的是向自己发送一条消息,当片段的消息处理程序循环运行时,该消息将被处理。