在android中编程线程和处理程序?
我已经执行了以下代码
public class Activity_Threads_Handler extends Activity {
private int mSec;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
startHeavyDutyStuff();
}
void startHeavyDutyStuff() {
// Here is the heavy-duty thread
Thread t = new Thread() {
public void run() {
try {
while (true) {
mSec = Calendar.getInstance().get(Calendar.MINUTE);
// Send update to the main thread
messageHandler.sendMessage(Message.obtain(
messageHandler, mSec));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
// Instantiating the Handler associated with the main thread.
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv.setText(msg.what);
}
};
}
上面的代码是在 TextView 上显示当前秒,不断更新它...
并且我在 logcat 中收到以下错误
致命异常:主要 android.content.res.Resources$NotFoundException:字符串资源 ID #x22 在 android.content.res.Resources.getText(Resources.java:201) 在 android.widget.TextView.setText(TextView.java:2857) 在 com.kpj4s.Threads_Handler.Activity_Threads_Handler$1.handleMessage(Activity_Threads_Handler.java:56) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:123) 在 android.app.ActivityThread.main(ActivityThread.java:3647) 在 java.lang.reflect.Method.invokeNative(本机方法) 在 java.lang.reflect.Method.invoke(Method.java:507) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 在dalvik.system.NativeStart.main(本机方法)
有人可以帮助我吗? 谢谢 :)
i have executed the following code
public class Activity_Threads_Handler extends Activity {
private int mSec;
TextView tv;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.TextView01);
startHeavyDutyStuff();
}
void startHeavyDutyStuff() {
// Here is the heavy-duty thread
Thread t = new Thread() {
public void run() {
try {
while (true) {
mSec = Calendar.getInstance().get(Calendar.MINUTE);
// Send update to the main thread
messageHandler.sendMessage(Message.obtain(
messageHandler, mSec));
Thread.sleep(1000);
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
};
t.start();
}
// Instantiating the Handler associated with the main thread.
private Handler messageHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
tv.setText(msg.what);
}
};
}
the above code is to display the current second on to a TextView continually updating it ...
and i got the following error in my logcat
FATAL EXCEPTION: main
android.content.res.Resources$NotFoundException: String resource ID #x22
at android.content.res.Resources.getText(Resources.java:201)
at android.widget.TextView.setText(TextView.java:2857)
at com.kpj4s.Threads_Handler.Activity_Threads_Handler$1.handleMessage(Activity_Threads_Handler.java:56)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:3647)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
at dalvik.system.NativeStart.main(Native Method)
can some one help me out pls ..
thanks :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是:
你想做什么?
问题是
msg.what
是一个整数,而setText
接收:一个String
或一个作为 android 资源的整数(例如 R .string.something)。因此,如果您想显示msg.what
内容,请使用tv.setText(String.valueOf(msg.what))
代替。The problem is:
What are you trying to do?
The problem is that
msg.what
is an integer andsetText
receives: either aString
or an integer that is an android resource (for instance R.string.something). Thus, if you want to show themsg.what
content usetv.setText(String.valueOf(msg.what))
instead.TextView.setText(int)
期望整数参数是资源标识符。您需要将其转换为String
,即您想要:TextView.setText(int)
expects the integer parameter to be a resource identifier. You need to convert it to aString
, i.e. you want: