有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

使用支持库从加载程序更新碎片列表后java应用程序崩溃

我有一个使用支持库v4的加载程序,它用于在一个包含两个片段的活动中加载ListView,一个包含ListView(作为ListFragment的扩展),另一个包含一个按钮(可以在加载程序执行工作时单击)

该实现与AsyncTaskLoader的Android文档中的实现非常相似,后者也通过加载程序创建一个ListView,但监视部分除外,我的实现不需要监视更改:http://developer.安卓.com/reference/安卓/content/AsyncTaskLoader.html

由于该应用程序支持API级别8,我将使用FragmentActivity::getSupportLoaderManager方法按照文档中的建议启动加载程序,以保持支持

http://developer.安卓.com/reference/安卓/support/v4/app/FragmentActivity.html

When using this class as opposed to new platform's built-in fragment and loader support, you must use the getSupportFragmentManager() and getSupportLoaderManager() methods respectively to access those features.

作为从片段启动的加载程序,我必须使用ListFragment::getActivity方法调用FragmentActivity::getSupportLoaderManager方法,从而使用以下代码启动加载程序:

getActivity().getSupportLoaderManager().initLoader(0,  null, this).forceLoad();

该应用程序在API高于8的情况下运行良好,但在级别8上,当加载程序在加载后尝试在UI上呈现列表时,它会崩溃(loader::onLoadFinished方法)

调试我发现ArrayAdapter<>;::正在适配器上调用addAll方法,这将确认问题出在UI的呈现上。此时,应用程序会被抛出到SamplingProfilerIntegration类,在该类中,试图在类的静态部分执行与快照相关的操作:

/** Whether or not a snapshot is being persisted. */
private static final AtomicBoolean pending = new AtomicBoolean(false);

static {
    samplingProfilerMilliseconds = SystemProperties.getInt("persist.sys.profiler_ms", 0);
    samplingProfilerDepth = SystemProperties.getInt("persist.sys.profiler_depth", 4);
    if (samplingProfilerMilliseconds > 0) {
        File dir = new File(SNAPSHOT_DIR);
        dir.mkdirs();
        // the directory needs to be writable to anybody to allow file writing
        dir.setWritable(true, false);
        // the directory needs to be executable to anybody to allow file creation
        dir.setExecutable(true, false);
        if (dir.isDirectory()) {
            snapshotWriter = Executors.newSingleThreadExecutor(new ThreadFactory() {
                    public Thread newThread(Runnable r) {
                        return new Thread(r, TAG);
                    }
                });
            enabled = true;
            Log.i(TAG, "Profiling enabled. Sampling interval ms: "
                  + samplingProfilerMilliseconds);
        } else {
            snapshotWriter = null;
            enabled = true;
            Log.w(TAG, "Profiling setup failed. Could not create " + SNAPSHOT_DIR);
        }
    } else {
        snapshotWriter = null;
        enabled = false;
        Log.i(TAG, "Profiling disabled.");
    }
}

这可能与文档中提到的蜂窝版本之前的UI呈现的具体行为有关,但我想不出是什么

http://developer.安卓.com/reference/安卓/support/v4/app/FragmentActivity.html

Prior to Honeycomb (3.0), an activity's state was saved before pausing. Fragments are a significant amount of new state, and dynamic enough that one often wants them to change between pausing and stopping. These classes throw an exception if you try to change the fragment state after it has been saved, to avoid accidental loss of UI state. However this is too restrictive prior to Honeycomb, where the state is saved before pausing. To address this, when running on platforms prior to Honeycomb an exception will not be thrown if you change fragments between the state save and the activity being stopped. This means that in some cases if the activity is restored from its last saved state, this may be a snapshot slightly before what the user last saw.


共 (0) 个答案