我正在尝试在最小API为10的情况下实现CursorLoader和LoaderManager。
但是,我在AsyncTaskLoader.class的第63行上不断收到IllegalArgument异常(发生异常的AsyncTaskLoader.class的源代码在下面和此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();
}
}
下面是错误堆栈:
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]
我测试了内容提供程序,并验证了查询方法从Sqlite数据库返回了有效的游标。以下是我尝试实现CursorLoader和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);
}
}
有人在这里看到我做错了什么吗?
谢谢。
该应用程序似乎使其
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
然后它在那里引发异常。
下面是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)
最佳答案
呼叫initLoader
后,尝试移动lv.setAdapter
。这将确保在onLoadFinished
与adapter.swapCursor(data)
关联之后,将调用SimpleCursorAdapter
(因此是ListView
)。
另外,请确保您的数据库具有一个名为_id
的主键列,因为CursorAdapter
否则将无法工作。 _id
需要作为CursorLoader
结果的一部分返回。否则将引发异常。
关于android - Android CursorLoader和LoaderManager错误,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/11803754/