gpt4 book ai didi

java - getWritableDatabase 函数导致错误

转载 作者:行者123 更新时间:2023-12-01 21:40:27 26 4
gpt4 key购买 nike

我正在创建一个名为 Mycontacts 的应用程序。这与 youtube 上的视频相同( https://www.youtube.com/watch?v=38DOncHIazs )。

但是这个getWritableDatabase()导致了错误

Non-static method 'getWritableDatabase()' cannot be referenced from a static context

public class addcontacts extends AppCompactActivity
{

EditText conname, conmobno, conemail;
Context context = this;
userDbHelper userdbhelp;
SQLiteDatabase sqLiteDatabase;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcontacts);
conname = (EditText) findViewById(R.id.Name);
conmobno = (EditText) findViewById(R.id.Contno);
conemail = (EditText) findViewById(R.id.Eid);
}

public void addContest(View view) {
String name = conname.getText().toString();
String mobno = conmobno.getText().toString();
String conem = conemail.getText().toString();
userdbhelp = new userDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();
userdbhelp.addinformation(name,mobno,conem, sqLiteDatabase);
Toast.makeText(getBaseContext(),"data saved",Toast.LENGTH_SHORT).show();
userdbhelp.close();
}
}

最佳答案

改变

userdbhelp = new userDbHelper(context);
sqLiteDatabase = userDbHelper.getWritableDatabase();

userdbhelp = new userDbHelper(context);
sqLiteDatabase = userdbhelp.getWritableDatabase();

您需要在实例 userdbhelp 上调用该方法,而不是在类 userDbHelper 上调用该方法。

关于java - getWritableDatabase 函数导致错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36536554/

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