FragmentRetainInstanceSupport.java 中的 UI 更改
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
可能是不同的 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 withgetActivity().runOnUIThread(some code)
. Just my suggestion.