我是java,android和sqlite的新手,我坚持这个。 我有列_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
这是日志。
答案 0 :(得分:12)
<强>加成强>
我相信你想使用localDBCrud.rawQuery()
,而不是db.rawQuery()
。
<强>原始强>
我猜你有一个NullPointerException。这里:
c.moveToFirst();
在使用c
初始化之前,您尝试使用db.rawQuery()
。
这里有一些逻辑错误:
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也做了很好的观察,也听从了他的意见。
答案 1 :(得分:1)
您应该将字符串值包装在单引号中。所以代替:
select sum(amount) from transaction_table where category = Salary ;
做的:
select sum(amount) from transaction_table where category = 'Salary';