我正在尝试更新数据库中的3列。但我总是在我的logcat中获得javanullexception。
以下是我更新它们的方法。
public void updateDetails(View v)
{
UpdateBtn = (ImageButton)findViewById(R.id.updateBtn);
HeightIn = (EditText)findViewById(R.id.heightIn);
HeightFt = (EditText)findViewById(R.id.heightFt);
Weight = (EditText)findViewById(R.id.weight);
UpdateBtn.setImageResource(com.example.gym.trainer.R.drawable.updatebtn2);
UpdateBtn.setClickable(false);
HeightIn.setEnabled(false);
HeightFt.setEnabled(false);
Weight.setEnabled(false);
//Toast.makeText(getBaseContext(), Integer.parseInt(id) + ", " + String.valueOf(HeightFt.getText()) + ", " + String.valueOf(HeightIn.getText()) + ", " + String.valueOf(Weight.getText()), Toast.LENGTH_LONG).show();
DBAdapter db = new DBAdapter(this);
boolean success = db.updateHeightWeight(Integer.parseInt(id), String.valueOf(HeightFt.getText()), String.valueOf(HeightIn.getText()), String.valueOf(Weight.getText()));
if(success == true)
Toast.makeText(getBaseContext(), "Update Successful", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getBaseContext(), "Update Failed", Toast.LENGTH_SHORT).show();
}
可以使用评论的吐司显示值,因此我非常确定有值。
在我的DBAdapter类中:
public boolean updateHeightWeight(int rowId, String heightft, String heightin, String weight)
{
ContentValues args2 = new ContentValues();
args2.put(KEY_HEIGHTFT, heightft);
args2.put(KEY_HEIGHTIN, heightin);
args2.put(KEY_WEIGHT, weight);
return db.update(DATABASE_TABLE, args2,
KEY_ROWID + "=" + rowId, null) > 0;// this is where the exception occurs
}
以下是我的DBAdapter类中的变量。
public static final String KEY_ROWID = "_id";
public static final String KEY_HEIGHTFT = "heightft";
public static final String KEY_HEIGHTIN = "heightin";
public static final String KEY_WEIGHT = "weight";
我桌子的结构:
private static final String DATABASE_CREATE =
"create table if not exists profiles (_id integer primary key autoincrement, "
+ "firstname VARCHAR not null, age VARCHAR not null, "
+ "heightft VARCHAR not null, heightin VARCHAR not null, weight VARCHAR not null, duration VARCHAR not null, bmi VARCHAR not null);";
关于如何解决我的问题的任何想法?谢谢!
答案 0 :(得分:1)
打赌你在尝试使用它之前没有打开你的数据库。这种事情很常见。
DBAdapter db = new DBAdapter(this);
db.open(); <-----------
boolean success = db.updateHeightWeight(Integer.parseInt(id), String.valueOf(HeightFt.getText()), String.valueOf(HeightIn.getText()), String.valueOf(Weight.getText()));