1

该应用程序有 2 个活动,第一个活动有 2 个字段,第二个活动有 1 个字段。第一个两个字段被插入到表中,即第一个活动。一旦插入前两个字段,用户就会被重定向到新活动。新活动有第三个字段,保存时应该在同一个表中更新。但是,在插入第一个两个值之后,当我在同一个表中更新第三个值时,我得到空指针异常。提前致谢。

这是我更新第三个值的第二个活动

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act2);
        ed3=(EditText)findViewById(R.id.data3id);
        updatebtn=(Button)findViewById(R.id.updatebtnid);


        final Intent intent = getIntent();
        mRowId=intent.getStringExtra("KEYROWID");


        updatebtn.setOnClickListener(new OnClickListener(){


            public void onClick(View v) {
                dba = new DBAdapter(v.getContext());

                String data3=ed3.getText().toString();


                dba.open();


                 String updatequery = "UPDATE "+ DBAdapter.DATABASE_TABLE+ " SET ";

                updatequery = updatequery + "KEY_DATA3='" +data3 +"'";
                Log.v("updatequery",updatequery);
                updatequery = updatequery + " WHERE _id = '" + mRowId + "';";
                Log.v("updatequery-new",updatequery);
                Log.v("test-1","update");
                try{
                    Log.v("test-2","update");
                    myDB.execSQL(updatequery);

                }catch(Exception e){
                    e.printStackTrace();
                }finally{
                    Log.v("test-3","update");

                    if(myDB != null)myDB.close();

                }


            }

这是我的 DBAdapter 类

public class DBAdapter {
    public static final String KEY_ROWID= "_id";
    public static final String KEY_DATA1="data1";
    public static final String KEY_DATA2="data2";
    public static final String KEY_DATA3="data3";


    private static String TAG="DBAdapter";

    static String DATABASE_NAME="update.db";
    static final String DATABASE_TABLE="update_table";
    private static int DATABASE_VERSION=2;


    private final Context context;
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    private static final String DATABASE_CREATE =
      "create table update_table(_id integer primary key autoincrement, "
    + "data1 varchar, data2 varchar, data3 varchar);";




    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }


        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS update_table");
            onCreate(db);
        }
    }




    public DBAdapter(Context ctx) {
        this.context = ctx;
    }


    public DBAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(context);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }


    public void close() {
        mDbHelper.close();
    }





    public long createddb(String data1,String data2,String data3) {

        ContentValues initialValues = new ContentValues();


        initialValues.put(KEY_DATA1, data1);
        initialValues.put(KEY_DATA2, data2);
        initialValues.put(KEY_DATA3, data3);


       return  mDb.insert(DATABASE_TABLE, null, initialValues);




    }



    public boolean deleteSaleseditdetails(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }




    public int fetchmaxId(){
        //return mDb.query(DATABASE_TABLE, new String[]{KEY_ROWID},null, null, null, null, null);
        String selectQuery = "SELECT max("+KEY_ROWID+") from " + DATABASE_TABLE ;
         Cursor c= mDb.rawQuery(selectQuery, null);
        c.moveToFirst();
        return c.getInt(0);
        //  cursor.moveToLast();


    }



    public boolean deletedb(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

以下是我进入 logcat 文件的错误

02-02 18:10:48.751: W/System.err(17003): java.lang.NullPointerException
02-02 18:10:48.761: W/System.err(17003):    at com.example.testupdate.Activity2$1.onClick(Activity2.java:62)
02-02 18:10:48.761: W/System.err(17003):    at android.view.View.performClick(View.java:2408)
02-02 18:10:48.761: W/System.err(17003):    at android.view.View$PerformClick.run(View.java:8816)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Handler.handleCallback(Handler.java:587)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-02 18:10:48.761: W/System.err(17003):    at android.os.Looper.loop(Looper.java:123)
02-02 18:10:48.761: W/System.err(17003):    at android.app.ActivityThread.main(ActivityThread.java:4627)
02-02 18:10:48.761: W/System.err(17003):    at java.lang.reflect.Method.invokeNative(Native Method)
02-02 18:10:48.761: W/System.err(17003):    at java.lang.reflect.Method.invoke(Method.java:521)
02-02 18:10:48.761: W/System.err(17003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-02 18:10:48.761: W/System.err(17003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-02 18:10:48.761: W/System.err(17003):    at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

3

问题myDB出在null. 我在您的代码中看不到这一点,但请检查您的myDB字段是否在某处初始化。如果没有,那就是导致您的NullPointerException.

顺便说一句,您正在检查是否myDB不是null为了知道是否应该关闭它:

if(myDB != null)myDB.close();

但是你打电话时没有做同样的检查myDB.execSQL(updatequery);

于 2013-02-02T13:01:01.007 回答