我有一个Activity
类的xml使用另一个类caled" helper",在帮助器中我通过发送TextView
的上下文来控制Activty
1}} class。
问题是在帮助器中我使用线程来做某事,每次它都这样做我想改变textView的文本(它在循环中做了几次)。
堆栈跟踪错误:
03-12 18:30:55.533: E/AndroidRuntime(5317): FATAL EXCEPTION: Thread-183
03-12 18:30:55.533: E/AndroidRuntime(5317): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4746)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:823)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.view.View.requestLayout(View.java:15473)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.widget.TextView.checkForRelayout(TextView.java:6452)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.widget.TextView.setText(TextView.java:3696)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.widget.TextView.setText(TextView.java:3554)
03-12 18:30:55.533: E/AndroidRuntime(5317): at android.widget.TextView.setText(TextView.java:3529)
03-12 18:30:55.533: E/AndroidRuntime(5317): at com.example.snake.GameView.AllChecks(GameView.java:175)
03-12 18:30:55.533: E/AndroidRuntime(5317): at com.example.snake.GameView.run(GameView.java:129)
03-12 18:30:55.533: E/AndroidRuntime(5317): at java.lang.Thread.run(Thread.java:856)
编辑:我特别错误的堆栈跟踪,我现在编辑它。虽然我通常得到我的答案,你不能从一个其他线程控制文本视图
答案 0 :(得分:2)
每个视图都有一个post method,将在UI线程上调用。
textView.post(new Runnable() {
@Override
public void run() {
textView.setText("something");
}
});
但是你的问题就在其他地方,只要你有NullPointerException