开发者

What in this method is leading to 'force close'?

开发者 https://www.devze.com 2023-02-17 01:05 出处:网络
I need to delete a row from the database table and then subsequently shift the following rows above. public void updateDraftsAfterDelete(long rowId)

I need to delete a row from the database table and then subsequently shift the following rows above.

public void updateDraftsAfterDelete(long rowId)
{
        String txt="",rid="",sto="",est="",sin="";
        boolean b=true;
        Cursor cursor = db.query(DATABASE_DRAFTS_TABLE, new String[] {
                KEY_DRAFTS_ROWID,
                KEY_DRAFTS_SENDTO,
                KEY_DRAFTS_TXT,
                KEY_DRAFTS_ENCST,
                KEY_DRAFTS_SIN }, 
          null, null, null, null, null);

    cursor.moveToPosition((int)(rowId+1));

    ContentValues values = new ContentValues();

    for(int i=(int)rowId;i<cursor.getCount();i++)
    {
        rid=new String(""+((Integer.parseInt(cursor.getString(0)))-1));
        sto=cursor.getString(1);
        txt=cursor.getString(2);
        est=cursor.getString(3);
        sin=cursor.getString(4);

    values.put(KEY_DRAFTS_SENDTO,sto);
        values.put(KEY_DRAFTS_TXT,txt);
        values.put(KEY_DRAFTS_ENCST, est);
        values.put(KEY_DRAFTS_SIN, sin);


        b=db.update(DATABASE_DRAFTS_TABLE, values, 
                KEY_DRAFTS_ROWID + "=" + rid, null) > 0;

        cursor.moveToNext();
    }
    cursor.moveToLast();

    开发者_如何学JAVArid=cursor.getString(0);

    Toast.makeText(null, rid, Toast.LENGTH_LONG).show();

        b=db.delete(DATABASE_DRAFTS_TABLE, KEY_DRAFTS_ROWID + "=" + rid, null) >0;
}

On running this, its replacing the rows properly but not deleting the final row. What is the problem? Thanks.


You need to pass a context object to the call to Toast.makeText, you cannot pass null, this will throw a NullPointerException and force close.

change the line

Toast.makeText(null, rid, Toast.LENGTH_LONG).show();

to:

Context context = getApplicationContext();
Toast.makeText(context, rid, Toast.LENGTH_LONG).show();
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号