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();

}

コメント付きのトーストを使用して値を表示できるので、値があると確信しています。

私の 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 に答える