gpt4 book ai didi

android - NullPointerException openOrCreateDatabase Android

转载 作者:行者123 更新时间:2023-11-30 04:17:13 27 4
gpt4 key购买 nike

我在打开数据库时在我的 EhActivity 类中遇到 NullPointerException

 public class EHActivity extends Activity {
ArrayList<String>ques= new ArrayList<String>();
ArrayList<String>ans=new ArrayList<String>();
SQLiteDatabase db2;

public ArrayList<String> getQues()
{
db2=openOrCreateDatabase("interview.db", MODE_PRIVATE, null);
db2.execSQL("create table eh (qid int primary key,ques varchar,ans char)");
db2.execSQL("insert into subject values(1,'What is an exception?','A1 '))");
db2.execSQL("insert into subject values(2,'What is error?','A2 '))");
Cursor c=db2.rawQuery("select * from eh",null);
while(c.moveToNext())
{
ques.add(c.getString(1));
}
return ques;
}
}

日志猫 View 是:

03-21 14:06:15.910: E/AndroidRuntime(955): java.lang.NullPointerException 03-21 14:06:15.910: E/AndroidRuntime(955): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203) 03-21 14:06:15.910: E/AndroidRuntime(955): at interview.android.EHActivity.getQues(EHActivity.java:19)

最佳答案

尽量不要发送 null..openOrCreateDatabase 对此有另一种方法..

db2=openOrCreateDatabase("interview.db", MODE_PRIVATE);

openDatabase("interview.db", MODE_PRIVATE, SQLiteDatabase.CREATE_IF_NECESSARY)

关于android - NullPointerException openOrCreateDatabase Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9801337/

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