gpt4 book ai didi

java - getWritableDatabase() 中的 nullPointerException

转载 作者:行者123 更新时间:2023-11-29 05:41:11 26 4
gpt4 key购买 nike

此处为 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com