0

我收到以下错误日志:

05-29 20:57:29.886: D/AndroidRuntime(359): Shutting down VM
05-29 20:57:29.896: W/dalvikvm(359): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-29 20:57:29.896: E/AndroidRuntime(359): Uncaught handler: thread main exiting due to uncaught exception
05-29 20:57:29.906: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dd.qsg/com.dd.qsg.QSGActivity}: java.lang.NullPointerException
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.os.Looper.loop(Looper.java:123)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread.main(ActivityThread.java:4363)
05-29 20:57:29.906: E/AndroidRuntime(359):  at java.lang.reflect.Method.invokeNative(Native Method)
05-29 20:57:29.906: E/AndroidRuntime(359):  at java.lang.reflect.Method.invoke(Method.java:521)
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
05-29 20:57:29.906: E/AndroidRuntime(359):  at dalvik.system.NativeStart.main(Native Method)
05-29 20:57:29.906: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.dd.qsg.DatabaseHandler.getLongestSmoke(DatabaseHandler.java:85)
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.dd.qsg.QSGActivity.onCreate(QSGActivity.java:40)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-29 20:57:29.906: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
05-29 20:57:29.906: E/AndroidRuntime(359):  ... 11 more
05-29 20:57:29.926: I/dalvikvm(359): threadid=7: reacting to signal 3
05-29 20:57:29.926: E/dalvikvm(359): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
05-29 20:57:31.206: I/Process(359): Sending signal. PID: 359 SIG: 9

我不知道如何解决这个问题。它在哪里告诉我我需要修复一些东西?

这是 DatabaseHandler.java 的第 85 行:

theCount = context.getResources().getString(R.string.na);

继承人的整个功能:

public String getLongestSmoke(Context context) {
    String theCount = "";  
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor cursor = db.rawQuery("SELECT millis FROM records ORDER BY CAST(millis as SIGNED) DESC", null);

    if (cursor.getCount() > 0) {
        cursor.moveToFirst();    
        theCount = cursor.getString(0);
    }
    else {
        theCount = context.getResources().getString(R.string.na);
    }

    cursor.close();        
    return humanTime(theCount);
}

这是使用此函数的主要活动的 onCreate 方法中的行:

    longestGone.setText(getString(R.string.longest_gone) + " " +  db.getLongestSmoke(null));

之前它为空时工作正常,现在突然停止了。我该怎么做?

在我对 DatabaseHandler 类的 onCreate 进行以下更改之前,这一切正常:

    String CREATE_ACHIEVEMENTS_TABLE = "CREATE TABLE achievements ("
             + "id INTEGER PRIMARY KEY,"
             + "name VARCHAR,"
             + "type VARCHAR,"
             + "value VARCHAR,"
             + "completed VARCHAR"
             + ")";              

    db.execSQL(CREATE_ACHIEVEMENTS_TABLE);      

    db.execSQL("insert into achievements (name, type, value, completed) values ('30 minutes', 'time', '1800000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('1 hour', 'time', '3600000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('2 hours', 'time', '7200000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('6 hours', 'time', '21600000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('12 hours', 'time', '43200000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('1 day', 'time', '86400000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('2 days', 'time', '172800000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('1 week', 'time', '604800000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('2 weeks', 'time', '1209600000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('1 month', 'time', '2592000000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('2 months', 'time', '5184000000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('6 months', 'time', '15552000000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('1 year', 'time', '31536000000', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('Set a record', 'milestone', '', 'no')");
    db.execSQL("insert into achievements (name, type, value, completed) values ('Beat top record', 'milestone', '', 'no')");

我还将它添加到 onUpgrade 中:

    db.execSQL("DROP TABLE IF EXISTS achievements");
4

3 回答 3

4

这里:

05-29 20:57:29.906: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.dd.qsg.DatabaseHandler.getLongestSmoke(DatabaseHandler.java:85)

我告诉您,在 DatabaseHelper.java 文件的第 85 行,您有一个无法访问的空值。

编辑:

根据您编辑的帖子,这是一行:

theCount = context.getResources().getString(R.string.na);

context为空并导致 NPE。

于 2012-05-29T21:02:03.193 回答
3
05-29 20:57:29.906: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
05-29 20:57:29.906: E/AndroidRuntime(359):  at com.dd.qsg.DatabaseHandler.getLongestSmoke(DatabaseHandler.java:85)

有你的线索。DatabaseHandler.java 第 85 行的某些内容试图取消对空引用的引用。

根据您的编辑,我猜那context是空的。

根据您的第二次编辑:

db.getLongestSmoke(null)

null作为context参数传入。传入一个有效的Context代替。尝试:

db.getLongestSmoke(this)

此调用来自哪里this的 Activity 。onCreate

于 2012-05-29T21:02:10.217 回答
-3

看起来 DatabaseHandler 为空

于 2012-05-29T21:02:46.903 回答