gpt4 book ai didi

java - 找不到NullPointerException的原因,日志没有按要求输出

转载 作者:行者123 更新时间:2023-12-01 13:19:50 25 4
gpt4 key购买 nike

我收到以下错误:

03-02 18:11:00.307: E/AndroidRuntime(2228): FATAL EXCEPTION: main
03-02 18:11:00.307: E/AndroidRuntime(2228): Process: com.dionsoft.dailymacrotracker, PID: 2228
03-02 18:11:00.307: E/AndroidRuntime(2228): java.lang.RuntimeException: Unable to resume activity {com.dionsoft.dailymacrotracker/com.dionsoft.dailymacrotracker.MainActivity}: java.lang.NullPointerException
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2788)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2817)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2250)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.access$800(ActivityThread.java:135)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.os.Looper.loop(Looper.java:136)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.main(ActivityThread.java:5017)
03-02 18:11:00.307: E/AndroidRuntime(2228): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 18:11:00.307: E/AndroidRuntime(2228): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 18:11:00.307: E/AndroidRuntime(2228): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
03-02 18:11:00.307: E/AndroidRuntime(2228): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
03-02 18:11:00.307: E/AndroidRuntime(2228): at dalvik.system.NativeStart.main(Native Method)
03-02 18:11:00.307: E/AndroidRuntime(2228): Caused by: java.lang.NullPointerException
03-02 18:11:00.307: E/AndroidRuntime(2228): at com.dionsoft.dailymacrotracker.MainActivity.onResume(MainActivity.java:30)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1192)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.Activity.performResume(Activity.java:5310)
03-02 18:11:00.307: E/AndroidRuntime(2228): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2778)
03-02 18:11:00.307: E/AndroidRuntime(2228): ... 12 more

这是 MainActivity 的代码:

public class MainActivity extends Activity {

private MainDS datasource;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

@Override
protected void onResume() {
super.onResume();
Map<String, Integer> todaysTotals = datasource.getTodaysTotals();
}

}

这是 MainDS 的代码:

public class MainDS {

private SQLiteDatabase database;
private SQLiteHelper dbHelper;

public MainDS(Context context) {
dbHelper = new SQLiteHelper(context);
}

public void open() throws SQLException {
database = dbHelper.getWritableDatabase();
}

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

public Map<String, Integer> getTodaysTotals () {
String sql = "select sum(" + SQLiteHelper.COLUMN_CALORIES + "), " +
"sum(" + SQLiteHelper.COLUMN_PROTEIN + "), " +
"sum(" + SQLiteHelper.COLUMN_CARBS + "), " +
"sum(" + SQLiteHelper.COLUMN_FAT + ") " +
"from " + SQLiteHelper.TABLE_RECORDS + " " +
"where date(" + SQLiteHelper.COLUMN_DATE + ", 'unixepoch') = date('now')";
Log.d("getTodaysTotals", sql);
Cursor cursor = database.rawQuery(sql, null);

Map<String, Integer> totals = new HashMap<String, Integer>();

if (cursor != null) {
totals.put("todays_calories", cursor.getInt(0));
totals.put("todays_protein", cursor.getInt(1));
totals.put("todays_carbs", cursor.getInt(2));
totals.put("todays_fat", cursor.getInt(3));
}

cursor.close();

return totals;
}

}

我不知道如何解决这个问题,因为它没有告诉我需要修复什么。

此外,它根本不记录 MainDS 中 Log.d 函数的 sql。

如有任何帮助,我们将不胜感激。

最佳答案

你错过了

datasource = new MainDS(this); 

在onCreate中

private MainDS datasource;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

private MainDS datasource;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
datasource = new MainDS(this);
setContentView(R.layout.activity_main);
}

关于java - 找不到NullPointerException的原因,日志没有按要求输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22135041/

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