14

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

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

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

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

这是我的 Fragment 的样子,为简洁起见稍作编辑:

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()从上面删除了黑客并添加了这个:

@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);
}
4

1 回答 1

2

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

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

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

于 2011-09-20T14:42:43.777 回答