03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: FATAL EXCEPTION: main
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: Process: tech.androidstudio.handlerdemotimer, PID: 22891
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime:
android.content.res.Resources$NotFoundException: String resource ID #0x0
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at android.content.res.Resources.getText(Resources.java:274)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at android.widget.TextView.setText(TextView.java:4128)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at tech.androidstudio.handlerdemotimer.MainActivity$1.handleMessage(MainActivity.java:30)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at android.os.Looper.loop(Looper.java:135)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5310)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Native Method)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:372)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)
03-16 21:41:42.466 22891-22891/tech.androidstudio.handlerdemotimer E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
mainHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
switch (what){
case 1:
int arg1 = msg.arg1;
Log.d("Kodulf","Handler arg1="+arg1);
Log.d("Kodulf","TestView ="+mTextView.getText().toString());
mTextView.setText(arg1);
break;
}
}
};
原因分析:因为设置的是setText 里面应该是String 类型的,而arg1 是int 类型的
解决方法:将int 类型的arg1转换为String 类型的.
mainHandler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
int what = msg.what;
switch (what){
case 1:
int arg1 = msg.arg1;
Log.d("Kodulf","Handler arg1="+arg1);
Log.d("Kodulf","TestView ="+mTextView.getText().toString());
mTextView.setText(String.valueOf(arg1));
break;
}
}
};