gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 12:48:39 25 4
gpt4 key购买 nike

我有一个使用支持库 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com