0

我想获取最近输入的第1列数据,所以我实现了以下代码,但它失败了。

我已经研究了这个站点中关于“SELECT MAX(_id) FROM”的许多问题和答案,但似乎仍然没有成功的方法(Logcat 如下图所示):

数据库助手

private static final String TABLE_CREATE = 
                    "CREATE TABLE " + TABLE_NAME + " ( " +
                    " _id   int, " + ...

public long getMaxID() 
{
    int id = 0;
    final String MY_QUERY = "SELECT MAX(_id) AS _id FROM " + TABLE_NAME;
    Cursor mCursor  = database.rawQuery(MY_QUERY, null);  //LINE 80

    if (mCursor.getCount() > 0) 
    {
        mCursor.moveToFirst();
        id = mCursor.getInt(mCursor.getColumnIndex("_id"));
    }
    return id;

活动简介:

DataBaseHelper databaseConnector = new DataBaseHelper(Profile.this);
long ID = databaseConnector.getMaxID(); 
try
    {
        new LoadDataTask().execute(ID);
    }
    catch (SQLiteException e)
    {
        System.err.println("Exception Message: " + e.getMessage());
    }  
 } 

日志猫:

08-30 00:22:00.105: E/AndroidRuntime(8658): FATAL EXCEPTION: main
08-30 00:22:00.105: E/AndroidRuntime(8658): java.lang.RuntimeException: Unable to resume activity {com.abc.abc/com.abc.abc.Profile}: java.lang.NullPointerException
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2616)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2644)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2130)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.os.Looper.loop(Looper.java:137)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.main(ActivityThread.java:4898)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at java.lang.reflect.Method.invokeNative(Native Method)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at java.lang.reflect.Method.invoke(Method.java:511)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at dalvik.system.NativeStart.main(Native Method)
08-30 00:22:00.105: E/AndroidRuntime(8658): Caused by: java.lang.NullPointerException
08-30 00:22:00.105: E/AndroidRuntime(8658):     at com.abc.abc.Abc_Measures_DataBaseHelper.getMaxID(Abc_Measures_DataBaseHelper.java:80)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at com.abc.abc.Profile.onResume(Profile.java:735)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1199)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.Activity.performResume(Activity.java:5280)
08-30 00:22:00.105: E/AndroidRuntime(8658):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2606)
08-30 00:22:00.105: E/AndroidRuntime(8658):     ... 12 more
4

1 回答 1

1
Cursor mCursor  = database.rawQuery(MY_QUERY, null);  //LINE 80

databasenull( Caused by: java.lang.NullPointerException)。您需要database在使用它之前初始化您的数据成员。

于 2013-08-29T16:51:04.407 回答