java - 使用支持库从加载器更新 FragmentList 后应用程序崩溃

标签 java android android-listview android-listfragment android-support-library

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

该实现与 AsyncTaskLoader 的 Android 文档中提供的实现非常相似,它也是通过加载器创建一个 ListView,但监视部分除外,我的实现不需要监视更改:http://developer.android.com/reference/android/content/AsyncTaskLoader.html

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

http://developer.android.com/reference/android/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.

作为从 Fragment 启动的加载器,我必须使用 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.");
    }
}

这可能与文档中所述的 Honeycomb 版本之前的 UI 渲染的特定行为有关,但我想不出是什么。

http://developer.android.com/reference/android/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.

最佳答案

我发现导入到项目中的支持​​库 v4 和 v7 不支持 ArrayAdapter<>::addAll() 方法,这导致应用程序崩溃。

这个问题与这个问题相关,提出的解决方案适合解决我的问题:

ListViews - how to use ArrayAdapter.addAll() function before API 11?

因此,解决方案是实现我自己的 ArrayAdapter 类版本,以便 Honeycomb 之前的 Android 版本可以使用它。

关于java - 使用支持库从加载器更新 FragmentList 后应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24435869/

相关文章:

java - Maven/Eclipse - 将项目更新到 Java7 搞砸了源文件夹

android - Kotlin 错误 - 委托(delegate)调用链中有一个循环

java - EnumSet 可分割

java - 如何在java中按下按钮时更改按钮的颜色

android - Android Navigation Drawer中Framelayout的使用

android - 如何以编程方式突出显示 ListView 的项目?

java - Android:针对不同自定义对象的自定义 ArrayAdapter

android - 无法使用 SimpleAdapter、ViewBinder 和 Asynctask 将图像从 ListView 设置到 ImageView

java - 在 matlab classdef 中导入 java 类

java - 登录