Android 为什么我的记录更新会出现空指针异常?
我正在创建记事本应用程序,希望更新笔记,但无法在sqlite数据库中记录。请检查我的更新查询 我正在共享错误详细信息Android 为什么我的记录更新会出现空指针异常?,android,sqlite,insert-update,Android,Sqlite,Insert Update,我正在创建记事本应用程序,希望更新笔记,但无法在sqlite数据库中记录。请检查我的更新查询 我正在共享错误详细信息 public class MainUpdateActivity extends Activity { SQLiteDatabase mydb1; EditText titleedit,notetextedit; Button update; @Override protected void onCreate(Bundle savedInstanceSta
public class MainUpdateActivity extends Activity {
SQLiteDatabase mydb1;
EditText titleedit,notetextedit;
Button update;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
titleedit=(EditText)findViewById(R.id.title);
notetextedit=(EditText)findViewById(R.id.notetext);
update=(Button) findViewById(R.id.update);
Intent myintent = getIntent();
String product7 = myintent.getStringExtra("product1");
String product8 = myintent.getStringExtra("product");
titleedit.setText(product7);
notetextedit.setText(product8);
update.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
mydb1.execSQL("UPDATE notes SET (title,notetext) values(?,?);",new String[]{titleedit.getText().toString(),notetextedit.getText().toString()});
Toast.makeText(getApplicationContext(), "note Updated", 3000).show();
Intent myIntent1 = new Intent(MainUpdateActivity.this,menu.class);
startActivity(myIntent1);
finish();
}
});
看起来像
mydb1.execSQL("UPDATE notes SET (title,notetext) values(?,?);",new String[]{titleedit.getText().toString(),notetextedit.getText().toString()});
mydb1
为空,您没有新建,并且initmydb1
您已经声明了SQLiteDatabase mydb1
,但没有初始化它。错误在这一行
mydb1.execSQL("UPDATE notes SET (title,notetext) values(?,?);",new String[]{titleedit.getText().toString(),notetextedit.getText().toString()});
您需要初始化
mydb1
注意:这里实际上不需要显式的.toString()
转换,因为字符串数组中的包含将自动执行隐式转换。是的,toString
可以被删除。否它不为null它有两条记录。@ramanrayat,我的意思是你不需要初始化mydb1
的值。对于exmaple,你应该使用SqlLiteOpenHelper.getWriteableDatabase()
来初始化它。但我仍然没有得到你的答案。如果可以的话,请更正我的代码。你的主要更新活动行63是什么?mydb1.execSQL(“更新注释集(标题,注释文本)值(?,);”,新字符串[]{titleedit.getText().toString(),notetextedit.getText().toString()});谢谢,我更正了:)