Android:设置TextView.setText(..)时出现奇怪错误;

时间:2011-06-27 10:22:21

标签: android widget

公共类游戏扩展活动     {         private static int res_health;         private static int res_wood;         private static int res_gold;         private static int res_steel;

    private static int stat_str;
    private static int stat_int;
    private static int stat_wit;
    private static int stat_maxHp;
    private static int stat_otvaga;

    private static final String queryLoad = "SELECT * FROM resc";

    private TextView txtHealth;

     @Override
       protected void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);


     }
     @Override
     public void onPause()
     {
      super.onPause();
     }

     @Override 
     public void onResume()
     {
         super.onResume();
     }

     public  Game()
     {

     TextView textHealth = new TextView(this);
     textHealth = (TextView)findViewById(R.id.strHealth);

     res_health = 25;
     res_wood = 0;
     res_gold = 0;
     res_steel = 0;

     stat_str = 4;
     stat_int = 3;
     stat_wit = 3;
     stat_maxHp = 25;
     stat_otvaga = 0;
     textHealth.setText(res_health);// TODO:ОШИБКА!
     }

错误是它仅在我使用TextView

时才要求强制关闭

                                       

4 个答案:

答案 0 :(得分:3)

请删除以下行。

TextView textHealth = new TextView(this);

并设置文字

textHealth.setText( “” + res_health);

使用public void game()而不是公共游戏()

查看完整答案

 private static int stat_str;
    private static int stat_int;
    private static int stat_wit;
    private static int stat_maxHp;
    private static int stat_otvaga;

    private static final String queryLoad = "SELECT * FROM resc";

    private TextView txtHealth;

     @Override
       protected void onCreate(Bundle savedInstanceState)
     {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.game);


     }
     @Override
     public void onPause()
     {
      super.onPause();
     }

     @Override 
     public void onResume()
     {
         super.onResume();
     }

     public void Game()
     {

    // TextView textHealth = new TextView(this);
     txtHealth = (TextView)findViewById(R.id.strHealth);

     res_health = 25;
     res_wood = 0;
     res_gold = 0;
     res_steel = 0;

     stat_str = 4;
     stat_int = 3;
     stat_wit = 3;
     stat_maxHp = 25;
     stat_otvaga = 0;
     txtHealth.setText(""+res_health);// TODO:ОШИБКА!
     }

答案 1 :(得分:2)

错误来自您使用TextView.setText(int)的事实,而这不是您想要做的事情。您提供的int是您想要显示的内容,但Android认为这是一个资源ID(cf:http://developer.android.com/reference/android/widget/TextView.html#setText%28int%29)。

尝试改为使用textHealth.setText(String.valueOf(res_health));

答案 2 :(得分:1)

尝试

textHealth.setText(""+res_health)

在你的例子中,它将它视为R.java文件的int val,因此dats y通过异常

答案 3 :(得分:1)

你混合'textHealth'和'txtHealth'。