0

DBにクエリを実行する次の方法があります

public String getCurrentMonthTotal() {
        String totalSum = "0.00";       
        Calendar cal = Calendar.getInstance();      
        int currentMonth = cal.get(Calendar.MONTH);
        Cursor cursor = database.query("expenses",
                new String[] { "SUM(\"amount\")" }, "expensemonth="
                        + currentMonth, null, null, null, null);
        if (cursor.moveToFirst()) {
            while (!cursor.isAfterLast()) {
                totalSum = cursor.getString(cursor.getColumnIndex("amount"));
                // do what ever you want here
                cursor.moveToNext();
            }
        }
        cursor.close();
        return totalSum;
    }

しかし、この時点に到達すると、アプリケーションはクラッシュします。理由はありますか?

Logcat トレース

12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.os.Looper.loop(Looper.java:137)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread.main(ActivityThread.java:4745)
12-17 02:46:37.893: E/AndroidRuntime(837):  at java.lang.reflect.Method.invokeNative(Native Method)
12-17 02:46:37.893: E/AndroidRuntime(837):  at java.lang.reflect.Method.invoke(Method.java:511)
12-17 02:46:37.893: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-17 02:46:37.893: E/AndroidRuntime(837):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-17 02:46:37.893: E/AndroidRuntime(837):  at dalvik.system.NativeStart.main(Native Method)
12-17 02:46:37.893: E/AndroidRuntime(837): Caused by: java.lang.NullPointerException
12-17 02:46:37.893: E/AndroidRuntime(837):  at com.technowizsol.easyexpensemanager.ExpenseList.onCreate(ExpenseList.java:21)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.Activity.performCreate(Activity.java:5008)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-17 02:46:37.893: E/AndroidRuntime(837):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-17 02:46:37.893: E/AndroidRuntime(837):  ... 11 more
12-17 02:49:03.202: E/Trace(885): error opening trace file: No such file or directory (2)
12-17 02:49:04.003: D/gralloc_goldfish(885): Emulator without GPU emulation detected.
4

1 に答える 1

0

LogCat エラーがなければ、推測するしかありません...しかし、これは正しくありません。

totalSum = cursor.getString(cursor.getColumnIndex("amount"));

Sum 列の名前は「金額」ではないと思います。1 行のループは必要ありません。試す:

if (cursor.moveToFirst()) {
    totalSum = cursor.getString(0); // You can use getInt()
}
于 2012-12-16T21:26:41.620 に答える