找不到NullPointerException的原因,log不按需要输出

时间:2014-03-02 23:19:24

标签: java android nullpointerexception

我收到以下错误:

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。

任何帮助都将不胜感激。

2 个答案:

答案 0 :(得分:1)

你错过了

datasource = new MainDS(this); 
创建

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

答案 1 :(得分:0)

下面:

datasource.getTodaysTotals();

您尝试使用数据源,但您从未初始化它! ==&GT; NPE