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

Android:设置TextView.setText(..)时出现奇怪错误;,android,widget,Android,Widget,公开课游戏拓展活动 { 私人静态健康; 私人静态室内木材; 私人静态积分金; 私人静钢 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 que

公开课游戏拓展活动 { 私人静态健康; 私人静态室内木材; 私人静态积分金; 私人静钢

    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时才要求强制关闭


请删除下面的行

TextView textHealth=新建TextViewthis

和用于设置文本

text-health.setText+res\u-health

使用公共无效游戏代替公共游戏

看到完整的答案了吗

 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:ОШИБКА!
     }

错误源于您正在使用的TextView.setTextint不是您想要的。您提供的int是您想要显示的,但Android认为这是一个资源ID cf:


尝试改用textHealth.setTextString.valueOfres\u health;试试看。

textHealth.setText(""+res_health)

在您的示例中,它将其视为R.java文件的int Val.DATY Y,通过异常

< P>混合了“TeXTHALTH”和“TXTHEALTH”。

请删除该TeXVIEW TeXTHALTHEY新TeXVIEW;并检查它。它工作。让我在LogCad中显示您的错误日志,您可以在LogCar中看到什么?您可以显示您的游戏吗?XML布局文件?即使我写这个textHealth.setText11;错误comes@cr3a70r:这当然是一个错误。您希望从错误而不是错误中得到什么?setText expect参数为字符串。与11textHealth.setTextString.ValueO11不同;如果使用11,则无需使用string.ValueOfn clik窗口->显示视图->如果找不到logcat然后单击其他。您将发现logcat没有混合。我没有使用类变量。我使用的是函数中的局部变量