2012-12-11 63 views
1

我正在嘗試更新數據庫中的3列。但我總是在我的logcat中獲得javanullexception。在android中更新表格

下面是我如何更新它們。

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

哪一行給你NullException? –

+0

return db.update(DATABASE_TABLE,args2,KEY_ROWID +「=」+ rowId,null)> 0;這一個先生 – ljpv14

回答

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

我真的很抱歉的錯誤。下次我會看看那些簡單的錯誤。謝謝! – ljpv14