gpt4 book ai didi

java - 由 : java. lang.NullPointerException 引起

转载 作者:行者123 更新时间:2023-12-01 19:04:14 26 4
gpt4 key购买 nike

我收到以下错误日志:

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);
}

这是使用此函数的主 Activity 的 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");

最佳答案

这里:

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 为 null 并导致 NPE。

关于java - 由 : java. lang.NullPointerException 引起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10806630/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com