我目前正在为 android 开发游戏。我想在我的类 gameView 上添加一个 textview。这是我的代码:
类 main.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<app.com.GameView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/game_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TextView
android:id = "@+id/score_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:paddingLeft="15dp"
/>
</FrameLayout>
这是类活动:
public class GameActivity extends Activity {
/** Called when the activity is first created. */
private app.com.GameView gameView ;
private TextView scoreText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gameView = (app.com.GameView)findViewById(R.id.game_view);
scoreText = (TextView)findViewById(R.id.score_text);
gameView.setScoreText(scoreText);
}
这是类 GameView :
private int score = 0;
private TextView scoreText;
public void setscoreText(TextView tv){
this.scoreText = tv;
}
但是当我使用 scoreText.setText(""+score) 时,却出现错误异常“CalledFromWrongThreadException: 只有创建视图层次结构的原始线程才能触摸它的视图。”?请帮我?