本文介绍了Android的CursorLoader和LoaderManager错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现对10的分钟API

一个CursorLoader和LoaderManager

不过,我不断收到关于AsyncTaskLoader.class的63号线(在异常发生的AsyncTaskLoader.class的源$ C ​​$ C的IllegalArgument异常如下,并在此<一个href=\"http://grep$c$c.com/file/repo1.maven.org/maven2/com.google.android/support-v4/r7/android/support/v4/content/AsyncTaskLoader.java#AsyncTaskLoader.LoadTask.onPostExecute%28java.lang.Object%29\"相对=nofollow> 链接

  / *运行在UI线程* /
@覆盖
保护无效onPostExecute(D数据){
    如果(调试)Log.v(TAG,这+onPostExecute);
    尝试{
        AsyncTaskLoader.this.dispatchOnLoadComplete(此,数据);
    } {最后
        done.countDown();
    }
}

下面是在错误的堆栈:

  AsyncTaskLoader $ LoadTask.onPostExecute(对象)行:63
AsyncTaskLoader $ LoadTask(ModernAsyncTask).finish(对象)线:461
ModernAsyncTask.access $ 500(ModernAsyncTask,对象)行:47
ModernAsyncTask $ InternalHandler.handleMessage(消息)线:474
ModernAsyncTask $ InternalHandler(处理器).dispatchMessage(消息)行:99
Looper.loop()行:137
ActivityThread.main(字符串[])行:4424
Method.invokeNative(对象,对象[],上课,下课[],类,整型,布尔)行:不可用[本机方法]
Method.invoke(对象,对象...)线:511
ZygoteInit $ MethodAndArgsCaller.run()行:784
ZygoteInit.main(字符串[])线:551
NativeStart.main(字符串[])行:不可用[本机方法]

我测试了我的内容提供商,我验证了查询方法返回从我SQLite数据库的有效光标。下面是我的活动源$ C ​​$ C,我想实现的CursorLoader和LoaderManager:

 进口android.database.Cursor;
进口android.os.Bundle;
进口android.support.v4.app.FragmentActivity;
进口android.support.v4.app.LoaderManager;
进口android.support.v4.content.CursorLoader;
进口android.support.v4.content.Loader;
进口android.support.v4.widget.SimpleCursorAdapter;
进口android.widget.ListView;公共类Screen1_MainData扩展FragmentActivity实现LoaderManager.LoaderCallbacks&LT;&光标GT; {    SimpleCursorAdapter适配器;
    LV的ListView;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.screen1_maindata);
        initLoader();
    }    无效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,则此)。        适配器=新SimpleCursorAdapter(这一点,R.layout.row,空,uiBindFrom,
                uiBindTo,0);
        lv.setAdapter(适配器);
    }@覆盖
公共装载机&LT;&光标GT; onCreateLoader(INT为arg0,ARG1捆绑){    的String [] =投影{Useful_Info_TableMetaData.USEFUL_INFO_COLUMN_NAME};    返回新CursorLoader(
            对此,//活动场景
            Useful_Info_TableMetaData.CONTENT_URI,投影,NULL,NULL,
            空值);
}    @覆盖
    公共无效onLoadFinished(装载机&LT;&光标GT;装载机,游标数据){
        adapter.swapCursor(数据);
    }    @覆盖
    公共无效onLoaderReset(装载机&LT;&光标GT;装载机){
        adapter.swapCursor(NULL);
    }
}

有谁看到这里的东西,我做错了?

感谢。


该应用程序似乎使它到

  @覆盖
公共无效onLoadFinished(装载机&LT;&光标GT;装载机,游标数据){
    adapter.swapCursor(数据);
}

,然后抛出异常那里。

下面是logcat的:

  08-03 23:33:20.736:E / AndroidRuntime(17256):致命异常:主要
08-03 23:33:20.736:E / AndroidRuntime(17256):java.lang.IllegalArgumentException异常:列'_id'不存在
08-03 23:33:20.736:E / AndroidRuntime(17256):在android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
08-03 23:33:20.736:E / AndroidRuntime(17256):在android.database.CursorWrapper.getColumnIndexOrThrow(CursorWrapper.java:78)
08-03 23:33:20.736:E / AndroidRuntime(17256):在android.support.v4.widget.CursorAdapter.swapCursor(CursorAdapter.java:344)
08-03 23:33:20.736:E / AndroidRuntime(17256):在android.support.v4.widget.SimpleCursorAdapter.swapCursor(SimpleCursorAdapter.java:326)


解决方案

尝试移动 initLoader 您的来电后, lv.setAdapter 。这将确保 onLoadFinished (因而 adapter.swapCursor(数据))将被调用的之后 SimpleCursorAdapter 已与相关联的ListView

此外,请确保您的数据库中有一个名为列 _id 为您的主键,为的CursorAdapter 荣获' ŧ否则工作。 _id 需要返回从 CursorLoader 的结果的一部分。否则会抛出异常。

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.

这篇关于Android的CursorLoader和LoaderManager错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-22 12:22