我在名为 GameLoop 的类中有此代码(类似于 LunarLander 示例的代码)
while( running ) {
Canvas canvas = null;
try {
canvas = mSurfaceHolder.lockCanvas(null);
long t = System.currentTimeMillis();
if( t - old[0] >= frame ) {
old[0] = t;
synchronized( mSurfaceHolder ) {
mGame.update();
mGame.onDraw(canvas);
}
}
}
finally {
// Do this in finally so that if an exception is thrown
// we don't leave the Surface in an inconsistent state
if( canvas != null ) mSurfaceHolder.unlockCanvasAndPost(canvas);
}
}
父类是GameView(参考mGame)。
当mGame.update()
试图调用他自己的announce
方法时,
public class GameView extends SurfaceView implements SurfaceHolder.Callback {
. . .
private void announce(int resId) {
synchronized( mSurfaceHolder ) {
tv_announcement.setText(getContext().getString(resId));
ObjectAnimator anim = ObjectAnimator.ofFloat(tv_announcement, "alpha", 0, 1);
anim.setDuration(ANNOUNCE_DURATION).start();
}
}
. . .
我得到这个例外:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
这里有两个线程,UI 线程,运行 GameView 并拥有 SurfaceView,以及 GameLoop,它是 Thread 的扩展,只运行更新绘制循环。
据我了解,异常告诉我只有 UI 线程可以触摸 TextView tv_announcement
。
但是不是announce
在 UI 线程中运行吗?我在这里想念什么?
这是announce后异常的logcat:
03-22 07:20:19.096: D/GameView(28250): + update(): state:IDLE, next state:CHOOSE_SIDE
03-22 07:20:19.096: D/GameView(28250): + announce(resId:2131361803)
03-22 07:20:19.096: W/dalvikvm(28250): threadid=10: thread exiting with uncaught exception (group=0xb3040180)
03-22 07:20:19.096: E/AndroidRuntime(28250): FATAL EXCEPTION: Thread-192
03-22 07:20:19.096: E/AndroidRuntime(28250): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4039)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:709)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:268)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.view.View.requestLayout(View.java:12675)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.widget.TextView.checkForRelayout(TextView.java:6773)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.widget.TextView.setText(TextView.java:3306)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.widget.TextView.setText(TextView.java:3162)
03-22 07:20:19.096: E/AndroidRuntime(28250): at android.widget.TextView.setText(TextView.java:3137)
03-22 07:20:19.096: E/AndroidRuntime(28250): at <package>.GameView.announce(GameView.java:436)
03-22 07:20:19.096: E/AndroidRuntime(28250): at <package>.GameView.update(GameView.java:314)
03-22 07:20:19.096: E/AndroidRuntime(28250): at <package>.GameView$GameLoop.run(GameView.java:495)
03-22 07:20:19.126: D/GameView(28250): + onWindowFocusChanged(hasWindowFocus:true)
03-22 07:20:19.126: D/GameView(28250): - onWindowFocusChanged()