我是
java,android和sqlite的新手,我坚持这个.
我有列_id,type,amount(int),category,description.我正在尝试获取特定类别的金额总和并将它们存储在int中.
我知道我需要提供错误,但它只是给我力量关闭.请帮助
我有列_id,type,amount(int),category,description.我正在尝试获取特定类别的金额总和并将它们存储在int中.
我知道我需要提供错误,但它只是给我力量关闭.请帮助
public class TransactionReport extends Activity { private static sqliteDatabase db; Cursor c; int amount; DbCrud localDbCrud = new DbCrud(this); @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.report); localDbCrud.open(); //localDatabase = localDbHelper.getWritableDatabase(); return this; c.moveToFirst(); do{ c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;",null); }while(c.moveToNext()); amount = c.getInt(0); c.close(); TextView ltxt = (TextView) findViewById(R.id.amt); ltxt.setText(""+amount); localDbCrud.close(); //localDatabase.close(); } }
如果详细解释,因为我是一个完整的菜鸟,我会尝试找到解决方案,但没有找到任何东西会有所帮助.谢谢提前
10-10 03:55:29.828: E/AndroidRuntime(10229): FATAL EXCEPTION: main 10-10 03:55:29.828: E/AndroidRuntime(10229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.hishighness.budgetracker/com.hishighness.budgetracker.TransactionReport}: java.lang.NullPointerException 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.os.Handler.dispatchMessage(Handler.java:99) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.os.Looper.loop(Looper.java:130) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread.main(ActivityThread.java:3687) 10-10 03:55:29.828: E/AndroidRuntime(10229): at java.lang.reflect.Method.invokeNative(Native Method) 10-10 03:55:29.828: E/AndroidRuntime(10229): at java.lang.reflect.Method.invoke(Method.java:507) 10-10 03:55:29.828: E/AndroidRuntime(10229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842) 10-10 03:55:29.828: E/AndroidRuntime(10229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600) 10-10 03:55:29.828: E/AndroidRuntime(10229): at dalvik.system.NativeStart.main(Native Method) 10-10 03:55:29.828: E/AndroidRuntime(10229): Caused by: java.lang.NullPointerException 10-10 03:55:29.828: E/AndroidRuntime(10229): at com.hishighness.budgetracker.TransactionReport.onCreate(TransactionReport.java:27) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 10-10 03:55:29.828: E/AndroidRuntime(10229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 10-10 03:55:29.828: E/AndroidRuntime(10229): ... 11 more
这是日志.
解决方法
加成
我相信你想使用localDBCrud.rawQuery(),而不是db.rawQuery().
我相信你想使用localDBCrud.rawQuery(),而不是db.rawQuery().
原版的
我猜你有一个NullPointerException.这里:
c.moveToFirst();
在使用db.rawQuery()初始化之前,您尝试使用c.
这里有一些逻辑错误:
c.moveToFirst(); do{ c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;",null); }while(c.moveToNext()); amount = c.getInt(0); c.close();
试试这个:
c = db.rawQuery("select sum(amount) from transaction_table where category = Salary ;",null); if(c.moveToFirst()) amount = c.getInt(0); else amount = -1; c.close();
@toadsky也做了很好的观察,也听从了他的建议.