在android中编程线程和处理程序?

发布于 2024-10-17 14:09:26 字数 2052 浏览 3 评论 0原文

我已经执行了以下代码

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巾帼英雄 2024-10-24 14:09:26

问题是:

tv.setText(msg.what);

你想做什么?

问题是 msg.what 是一个整数,而 setText 接收:一个 String 或一个作为 android 资源的整数(例如 R .string.something)。因此,如果您想显示 msg.what 内容,请使用 tv.setText(String.valueOf(msg.what)) 代替。

The problem is:

tv.setText(msg.what);

What are you trying to do?

The problem is that msg.what is an integer and setText receives: either a String or an integer that is an android resource (for instance R.string.something). Thus, if you want to show the msg.what content use tv.setText(String.valueOf(msg.what)) instead.

酒几许 2024-10-24 14:09:26

TextView.setText(int) 期望整数参数是资源标识符。您需要将其转换为 String,即您想要:

tv.setText(Integer.toString(msg.what)); 

TextView.setText(int) expects the integer parameter to be a resource identifier. You need to convert it to a String, i.e. you want:

tv.setText(Integer.toString(msg.what)); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文