当前位置: 代码迷 >> Android >> 关于使用handle产生The Application has stopped unexpectedly的有关问题
  详细解决方案

关于使用handle产生The Application has stopped unexpectedly的有关问题

热度:362   发布时间:2016-05-01 22:29:08.0
关于使用handle产生The Application has stopped unexpectedly的问题
源代码如下:

Java code
package game.showhand;import java.util.Timer;   import java.util.TimerTask; import android.app.Activity;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.widget.TextView;public class showhand extends Activity {     /** Called when the activity is first created. */         @Override    public void onCreate(Bundle savedInstanceState) {                super.onCreate(savedInstanceState);               setContentView(R.layout.main);         final Handler handler = new Handler()        {              int recLen;            TextView recTime = new TextView(showhand.this);           @Override        public void handleMessage(Message msg)         {          switch (msg.what)        {               case 1:               recLen++;         recTime.setText(recLen);        break;              }               super.handleMessage(msg);          }            };        TimerTask task = new TimerTask(){              public void run() {              Message message = new Message();                  message.what = 1;                  handler.sendMessage(message);                }              };         Timer timer = new Timer(true);        timer.schedule(task,1000, 1000);                    }}  




------解决方案--------------------
public void handleMessage(Message msg) 
{
switch (msg.what)
{
case 1:
recLen++;
recTime.setText(recLen);
break;
}


这段中recTime.setText(recLen);出错。recLen为int值,系统会到res去找对应的String,但是没有,所以报resources not found的错误。

建议将其改成:
recTime.setText(String.valueOf(recLen));

就一切OK了。
  相关解决方案