I am trying to implement a CursorLoader and LoaderManager on a min API of 10.
However, I keep getting an IllegalArgument Exception on Line 63 of AsyncTaskLoader.class (The source Code of the AsyncTaskLoader.class where the exception is happening is Below and at this link.
/* Runs on the UI thread */
@Override
protected void onPostExecute(D data) {
if (DEBUG) Log.v(TAG, this + " onPostExecute");
try {
AsyncTaskLoader.this.dispatchOnLoadComplete(this, data);
} finally {
done.countDown();
}
}
Below is the stack upon the error:
AsyncTaskLoader$LoadTask.onPostExecute(Object) line: 63
AsyncTaskLoader$LoadTask(ModernAsyncTask).finish(Object) line: 461
ModernAsyncTask.access$500(ModernAsyncTask, Object) line: 47
ModernAsyncTask$InternalHandler.handleMessage(Message) line: 474
ModernAsyncTask$InternalHandler(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 137
ActivityThread.main(String[]) line: 4424
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 511
ZygoteInit$MethodAndArgsCaller.run() line: 784
ZygoteInit.main(String[]) line: 551
NativeStart.main(String[]) line: not available [native method]
I tested my content provider and I validated that the query method returns a valid cursor from my Sqlite Database. Below is the source code of my activity that I am trying to implement the CursorLoader and LoaderManager:
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.SimpleCursorAdapter;
import android.widget.ListView;
public class Screen1_MainData extends FragmentActivity implements LoaderManager.LoaderCallbacks<Cursor> {
SimpleCursorAdapter adapter;
ListView lv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screen1_maindata);
initLoader();
}
void initLoader() {
String[] uiBindFrom = { Useful_Info_TableMetaData.USEFUL_INFO_COLUMN_NAME };
int[] uiBindTo = { R.id.name };
lv = (ListView) findViewById(R.id.screen1_MainListView);
getSupportLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.row, null, uiBindFrom,
uiBindTo, 0);
lv.setAdapter(adapter);
}
@Override
public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
String[] projection = { Useful_Info_TableMetaData.USEFUL_INFO_COLUMN_NAME };
return new CursorLoader(
this, // The Activity context
Useful_Info_TableMetaData.CONTENT_URI, projection, null, null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
}
Does anyone see something here that I am doing wrong?
Thanks.
The app seems to make it to
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
and then it throws the exception there.
Below is the logcat:
08-03 23:33:20.736: E/AndroidRuntime(17256): FATAL EXCEPTION: main
08-03 23:33:20.736: E/AndroidRuntime(17256): java.lang.IllegalArgumentException: column '_id' does not exist
08-03 23:33:20.736: E/AndroidRuntime(17256): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
08-03 23:33:20.736: E/AndroidRuntime(17256): at android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
08-03 23:33:20.736: E/AndroidRuntime(17256): at android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:344)
08-03 23:33:20.736: E/AndroidRuntime(17256): at android.support.v4.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:326)
Try moving initLoader
after your call to lv.setAdapter
. This will ensure that onLoadFinished
(and thus adapter.swapCursor(data)
) will be called after the SimpleCursorAdapter
has been associated with the ListView
.
Also, make sure your database has a column named _id
for your primary key, as the CursorAdapter
won't work otherwise. _id
needs to be returned as a part of the results from the CursorLoader
's. Otherwise an exception will be thrown.
来源:https://stackoverflow.com/questions/11803754/android-cursorloader-and-loadermanager-error