FragmentRetainInstanceSupport.java 中的 UI 更改

发布于 2024-12-29 19:44:26 字数 2535 浏览 0 评论 0原文

我正在使用 v4 兼容性应用程序的示例。我注意到在这一部分中:

mPosition++;
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);

如果我添加 TextView.setText()

mPosition++;
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);
mStatus.setText(String.valueOf(max)); // here, just to test stuff!

它会放弃并出现以下异常:

01-29 18:55:08.889: W/dalvikvm(14565): threadid=11: thread exiting with uncaught exception (group=0x40a791f8)
01-29 18:55:08.889: E/AndroidRuntime(14565): FATAL EXCEPTION: Thread-1035
01-29 18:55:08.889: E/AndroidRuntime(14565): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:701)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.checkForRelayout(TextView.java:6716)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3256)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3110)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3085)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at com.mytest.ui.PreActivity$RetainedFragment$1.run(PreActivity.java:111)

为什么会发生这种情况?或者更好的是,如何调整文档示例以使用 TextView 而不是 ProgressView

I'm using one of the examples for the v4 compatibility app. I've noticed that in this part:

mPosition++;
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);

if I add a TextView.setText():

mPosition++;
max = mProgressBar.getMax();
mProgressBar.setProgress(mPosition);
mStatus.setText(String.valueOf(max)); // here, just to test stuff!

it gives up with the following exception:

01-29 18:55:08.889: W/dalvikvm(14565): threadid=11: thread exiting with uncaught exception (group=0x40a791f8)
01-29 18:55:08.889: E/AndroidRuntime(14565): FATAL EXCEPTION: Thread-1035
01-29 18:55:08.889: E/AndroidRuntime(14565): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:3939)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:701)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.view.View.requestLayout(View.java:12555)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.checkForRelayout(TextView.java:6716)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3256)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3110)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at android.widget.TextView.setText(TextView.java:3085)
01-29 18:55:08.889: E/AndroidRuntime(14565):    at com.mytest.ui.PreActivity$RetainedFragment$1.run(PreActivity.java:111)

Why that happens? Or better yet, how do I adapt the docs example to use a TextView instead of a ProgressView?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

断桥再见 2025-01-05 19:44:26

可能是不同的 UI 线程再次调用保留中的 Fragment 的情况。可以使用 getActivity().runOnUIThread(some code) 来解决。只是我的建议。

May be the case of different UI-thread calling up again the Fragment in retain. It could be resolved with getActivity().runOnUIThread(some code). Just my suggestion.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文