我正在使用加载器,ActionBarSherlock和SqliteCursorLoader在片段中进行ListView。 ListView基本上显示了日期和GPS坐标的列表。当我在点击按钮后重新加载ListView以及时前进并显示相关数据时,会出现问题。
我在尝试使用forceLoad()时遇到了游标错误:
05-07 11:42:35.446: E/Cursor(9470): Invalid statement in fillWindow()
05-07 11:42:35.446: D/Row Count(9470): 0
05-07 11:42:35.467: D/AndroidRuntime(9470): Shutting down VM
05-07 11:42:35.467: W/dalvikvm(9470): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
05-07 11:42:35.467: E/AndroidRuntime(9470): Uncaught handler: thread main exiting due to uncaught exception
05-07 11:42:35.477: E/AndroidRuntime(9470): java.lang.IllegalArgumentException: column '_id' does not exist
05-07 11:42:35.477: E/AndroidRuntime(9470): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
05-07 11:42:35.477: E/AndroidRuntime(9470): at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:344)
05-07 11:42:35.477: E/AndroidRuntime(9470): at android.support.v4.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:326)
05-07 11:42:35.477: E/AndroidRuntime(9470): at android.support.v4.widget.CursorAdapter.changeCursor(CursorAdapter.java:315)
编辑(新错误代码):
05-07 14:24:30.046: W/dalvikvm(17284): threadid=21: thread exiting with uncaught exception (group=0x4001b188)
05-07 14:24:30.046: E/AndroidRuntime(17284): Uncaught handler: thread ModernAsyncTask #4 exiting due to uncaught exception
05-07 14:24:30.057: E/AndroidRuntime(17284): java.lang.RuntimeException: An error occured while executing doInBackground()
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.support.v4.content.ModernAsyncTask$3.done(ModernAsyncTask.java:137)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.lang.Thread.run(Thread.java:1096)
05-07 14:24:30.057: E/AndroidRuntime(17284): Caused by: java.lang.IllegalStateException: attempt to acquire a reference on a close SQLiteClosable
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:31)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:56)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1221)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1194)
05-07 14:24:30.057: E/AndroidRuntime(17284): at com.webnation.gpstracking.SQLiteCursorLoader.buildCursor(SQLiteCursorLoader.java:54)
05-07 14:24:30.057: E/AndroidRuntime(17284): at com.webnation.gpstracking.AbstractCursorLoader.loadInBackground(AbstractCursorLoader.java:38)
05-07 14:24:30.057: E/AndroidRuntime(17284): at com.webnation.gpstracking.AbstractCursorLoader.loadInBackground(AbstractCursorLoader.java:1)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.support.v4.content.AsyncTaskLoader.onLoadInBackground(AsyncTaskLoader.java:240)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:51)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.support.v4.content.AsyncTaskLoader$LoadTask.doInBackground(AsyncTaskLoader.java:40)
05-07 14:24:30.057: E/AndroidRuntime(17284): at android.support.v4.content.ModernAsyncTask$2.call(ModernAsyncTask.java:123)
05-07 14:24:30.057: E/AndroidRuntime(17284): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
我的rawSQL参数确实要求_id列,所以我不确定那里发生了什么。
我做了一些研究,其中大部分都说您需要按特定顺序关闭数据库和游标。由于我使用的是从数据库中检索数据的自定义游标加载器(SqliteCursorLoader),因此我不知道如何执行此操作。
这是我的onActivityCreated:
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
gps = new GPSTracker(getActivity());
mGPSDC = new GPSDateCoordinates();
CalcDate();
//enables buttons for next and previous months
GetPreviousFollowingMonths();
adapter= new SimpleCursorAdapter(getActivity(), R.layout.row, null, new String[] {
myDbHelper.COLUMN_DISTANCE, myDbHelper.COLUMN_FORMATTEDSTART },
new int[] { R.id.title, R.id.value },0);
lvReports.setAdapter(adapter);
btnDate.setText(getMonth(BaseMonth) + " " + String.valueOf(BaseYear));
getSherlockActivity().getSupportLoaderManager().initLoader(LOADER_ID, null, this);
// show location button click event
btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
CalculatePreviousNextMonths(false);
//getSherlockActivity().getSupportLoaderManager().restartLoader(LOADER_ID, null, ReportsFragment.this);
getSherlockActivity().getSupportLoaderManager().getLoader(LOADER_ID).forceLoad();
GetPreviousFollowingMonths();
btnDate.setText(getMonth(BaseMonth) + " " + String.valueOf(BaseYear));
}
});
// show location button click event
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
CalculatePreviousNextMonths(true);
//getSherlockActivity().getSupportLoaderManager().restartLoader(LOADER_ID, null, ReportsFragment.this);
getSherlockActivity().getSupportLoaderManager().getLoader(LOADER_ID).forceLoad();
GetPreviousFollowingMonths();
btnDate.setText(getMonth(BaseMonth) + " " + String.valueOf(BaseYear));
}
});
}
这是包含Loader函数的片段的其余部分;
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
loader = new SQLiteCursorLoader(getActivity().getApplicationContext(), myDbHelper,
myDbHelper.getSQLArrayOfDatesDistances(BaseYear,BaseMonth),null);
return loader;
}
@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
adapter.changeCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
adapter.changeCursor(null);
}