1

我正在尝试更新数据库中的 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();

}

可以使用注释的 toast 显示值,所以我很确定有值。

在我的 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);";  

关于如何解决我的问题的任何想法?谢谢!

4

1 回答 1

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()));
于 2012-12-11T14:52:29.873 回答