Android:CursorLoader在非最顶层的Fragment上崩溃

时间:2011-07-20 04:51:51

标签: android cursor android-fragments android-loadermanager

我有一些使用CursorLoader检索其内容的ListFragments。当用户向下钻取内容时,一个Fragment替换另一个Fragment(Activity保持不变)。但是如果内容在非最顶层的片段上发生变化,则应用程序崩溃:

E/AndroidRuntime(18830): FATAL EXCEPTION: main
E/AndroidRuntime(18830): java.lang.RuntimeException: Unable to resume activity {com.example.ExampleApp/com.example.ExampleApp.ExampleActivity}: java.lang.IllegalStateException: Content view not yet created
E/AndroidRuntime(18830):        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2120)
E/AndroidRuntime(18830):        at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2135)
E/AndroidRuntime(18830):        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:957)
E/AndroidRuntime(18830):        at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(18830):        at android.os.Looper.loop(Looper.java:130)
E/AndroidRuntime(18830):        at android.app.ActivityThread.main(ActivityThread.java:3683)
E/AndroidRuntime(18830):        at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(18830):        at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(18830):        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
E/AndroidRuntime(18830):        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
E/AndroidRuntime(18830):        at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(18830): Caused by: java.lang.IllegalStateException: Content view not yet created
E/AndroidRuntime(18830):        at android.support.v4.app.ListFragment.ensureList(ListFragment.java:328)
E/AndroidRuntime(18830):        at android.support.v4.app.ListFragment.setListShown(ListFragment.java:280)
E/AndroidRuntime(18830):        at android.support.v4.app.ListFragment.setListShownNoAnimation(ListFragment.java:266)
E/AndroidRuntime(18830):        at com.example.ExampleApp.FirstListFragment.onLoadFinished(FirstListFragment.java:102)
E/AndroidRuntime(18830):        at com.example.ExampleApp.FristListFragment.onLoadFinished(FirstListFragment.java:20)
E/AndroidRuntime(18830):        at android.support.v4.app.LoaderManagerImpl$LoaderInfo.callOnLoadFinished(LoaderManager.java:414)
E/AndroidRuntime(18830):        at android.support.v4.app.LoaderManagerImpl$LoaderInfo.reportStart(LoaderManager.java:298)
E/AndroidRuntime(18830):        at android.support.v4.app.LoaderManagerImpl.doReportStart(LoaderManager.java:751)
E/AndroidRuntime(18830):        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:512)
E/AndroidRuntime(18830):        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1129)
E/AndroidRuntime(18830):        at android.app.Activity.performStart(Activity.java:3791)
E/AndroidRuntime(18830):        at android.app.Activity.performRestart(Activity.java:3821)
E/AndroidRuntime(18830):        at android.app.Activity.performResume(Activity.java:3826)
E/AndroidRuntime(18830):        at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2110)
E/AndroidRuntime(18830):        ... 10 more

当后台更新更改内容时,或者当用户从主屏幕返回而另一个片段位于后端堆栈的顶部时,可能会发生这种情况。 onLoadFinished处理程序尝试更新不再存在的UI。但是为什么片段仍然会得到光标更新呢?

如果片段不可见,我通过中止更新来解决这个问题,但这似乎是错误的做法。

这就是我的片段的样子,为了简洁而编辑了一点:

public class FirstListFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        // Initialize empty cursor adapter.
        mAdapter = new ExampleCursorAdapter(getActivity(), null, 0);
        setListAdapter(mAdapter);

        // Start with a progress indicator
        setListShown(false);

        // Prepare the loader.  Either re-connect with an existing one, or start a new one.
        getLoaderManager().initLoader(EXAMPLE_LOADER_ID, null, this);
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        return new CursorLoader(getActivity(), ExampleProvider.CONTENT_URI, PROJECTION, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        // Do nothing if we're not visible.
        if(!isVisible()) {
            return;
        }

        // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        mAdapter.swapCursor(data);

        // The list should now be shown.
        if(isResumed()) {
            setListShown(true);
        } else {
            setListShownNoAnimation(true);
        }
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        // This is called when the last Cursor provided to onLoadFinished() above is about to be
        // closed.  We need to make sure we are no longer using it.
        mAdapter.swapCursor(null);
    }
}

isVisible()检查可以防止崩溃,但它不在我见过的任何示例代码中。这有什么不对?


编辑:果然,我结束了StaleDataException,旧视图试图使用关闭的游标。所以现在我在视图被破坏时会破坏LoaderManager。仍然不确定这是否正确,我无法重现StaleDataException。

我从上面删除了isVisible() hack并添加了这个:

@Override
public void onDestroyView() {
    super.onDestroyView();

    // The CursorLoader example doesn't do this, but if we get an update while the UI is
    // destroyed, it will crash.  Why is this necessary?
    getLoaderManager().destroyLoader(EXAMPLE_LOADER_ID);
}

1 个答案:

答案 0 :(得分:2)

这似乎是列表片段获取列表视图的问题。由于某种原因,除非显示片段,否则getListView()不会返回有效视图。我已经看到它在onStart()之前的生命周期中的任何内容上抛出了非法状态异常。

我解决问题的方法是在onCreateView()方法中调用loader。当加载器返回数据时,我检查活动是否已启动。如果是,那么我将它加载到列表视图中,如果不是我将它存储在局部变量中,然后在onStart()方法中加载数据。

这似乎是一个超级黑客,但它确实有效。