我正致力于从数据库中保存和提取数据,关键是从日历中选择的日期。
这是每天的一种记录重量数据。如果我已将所需数据输入到第A天,然后我继续提取第A天的数据,那么一切都很好。
如果我没有为A日输入任何内容并直接继续为A日提取数据,它将会崩溃。如何修改代码,以便用户可以安全地获取Toast说“如果没有DB中的记录,用户今天没有输入记录,没有找到记录”?
protected void onResume()
{
super.onResume();
try
{
new LoadDataTask().execute(record_day_int);
}
catch (SQLiteException e)
{
System.err.println("Exception Message: " + e.getMessage());
}
}
private class LoadDataTask extends AsyncTask<Long, Object, Cursor>
{
DataBaseHelper databaseConnector = new DataBaseHelper(Calendar_view.this);
@Override
protected Cursor doInBackground(Long... params)
{
databaseConnector.open();
return databaseConnector.getOneRecord(params[0]);
}
@Override
protected void onPostExecute(Cursor result)
{
super.onPostExecute(result);
result.moveToFirst();
int weight_kg01Index = result.getColumnIndex("weight_kg01");
weight_data.setText(""+result.getInt(weight_kg01Index));
result.close();
databaseConnector.close();
}
}
08-28 01:06:16.790: W/dalvikvm(19928): threadid=1: thread exiting with uncaught exception (group=0x412282a0)
08-28 01:06:16.790: E/AndroidRuntime(19928): FATAL EXCEPTION: main
08-28 01:06:16.790: E/AndroidRuntime(19928): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractCursor.checkPosition(AbstractCursor.java:418)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:136)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:68)
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.abc.abc.Calendar_view$LoadContactTask.onPostExecute(Calendar_view.java:233)
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.abc.abc.Calendar_view$LoadContactTask.onPostExecute(Calendar_view.java:1)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask.finish(AsyncTask.java:631)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask.access$600(AsyncTask.java:177)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.Handler.dispatchMessage(Handler.java:99)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.os.Looper.loop(Looper.java:137)
08-28 01:06:16.790: E/AndroidRuntime(19928): at android.app.ActivityThread.main(ActivityThread.java:4898)
08-28 01:06:16.790: E/AndroidRuntime(19928): at java.lang.reflect.Method.invokeNative(Native Method)
08-28 01:06:16.790: E/AndroidRuntime(19928): at java.lang.reflect.Method.invoke(Method.java:511)
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
08-28 01:06:16.790: E/AndroidRuntime(19928): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
08-28 01:06:16.790: E/AndroidRuntime(19928): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
做
if(result.moveToFirst()){
int weight_kg01Index = result.getColumnIndex("weight_kg01");
weight_data.setText(""+result.getInt(weight_kg01Index));
}
result.close();
如果光标为空,那么它将跳过