上的nullPointerException

上的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


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

最佳答案

尝试这个

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

关于java - getWritableDatabase()上的nullPointerException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17514933/

10-10 10:06