android - 在我的类(class) gameView 上添加一个 textView

标签 android android-canvas game-engine

我目前正在为 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>

这是类 Activity :

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:只有创建 View 层次结构的原始线程才能触及它的 View 。”?请帮帮我好吗?

最佳答案

您不能使用整数设置文本。

首先需要将其转换为字符串。最简单的方法是:

scoreText.setText(score + "");

scoreText.setText(Integer.toString(score));

关于android - 在我的类(class) gameView 上添加一个 textView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11983037/

相关文章:

android - DDMS View 文件资源管理器中的数据目录

android - 在 Canvas 上动画绘制圆/弧

Android View.invalidate(Rect) 两个设备之间的不同行为

math - 从欧拉位置和旋转计算前向和向上矢量?

c - 为什么玩家在改变方向时有时会向错误的方向移动

java - 如何防止用户读取 Java 中的变量?

Android 应用仅在某些设备上出现在 Google Play 搜索中

android - 截图

python - 在 Pygame 中检测鼠标悬停在图像上

Android AlertDialog按钮定位