java.lang.RuntimeException:无法在线程内创建处理程序

发布于 2025-01-06 17:31:04 字数 1678 浏览 4 评论 0原文

我收到错误...无法在未调用 Looper.prepare() 的线程内创建处理程序

实际上我的情况是这样的: 我正在尝试使用我从 Android 开发人员那里获取的“BluetoothChat”示例代码。我的任务是设置一个应用程序在连接远程设备后自动运行并发送消息...我想你们都知道我想说什么... 我有字符串消息,我希望应用程序每秒向远程设备发送一次:

String helloString[] = {"hello person"," hi there", "hola hola", "yau yau..."};

在这里,我尝试更改代码的某些部分,我认为应用程序会执行我想要的操作,但失败...:(

private void setupChat() 
    {
    Log.d(TAG, "setupChat()");

    Thread output = new Thread()
    {
        public void run()
    {
            while (true)
            {
                for(int i = 0; i < 4; i++)
                {

                    //Sending helloStrings for the device
                    message = helloString[i];       
                    sendMessage(message);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                }
            }
    }
    };
    output.start();

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this, mHandler);


}

/**
     * Sends a message.
     * @param message  A string of text to send.
     */   
    private void sendMessage(String message) 
    {
        // TODO Auto-generated method stub
        // Check that we're actually connected before trying anything
        if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) 
        {
            Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();           
            return;
        }

        byte[] send = message.getBytes();
        mChatService.write(send);

    }

I'm getting errors...Can't create handler inside thread that has not called Looper.prepare()

Actually my case is like this:
I'm trying to use the example code of "BluetoothChat" that I took from Android developer. My mission is set an app running and sending messages automatically after the remote device is connected... I think you all know what I'm trying to say...
I have String messages that I want the app send every second for remote device:

String helloString[] = {"hello person"," hi there", "hola hola", "yau yau..."};

Here I tried to change some part of the code where i think an app will do what I want with failed... :(

private void setupChat() 
    {
    Log.d(TAG, "setupChat()");

    Thread output = new Thread()
    {
        public void run()
    {
            while (true)
            {
                for(int i = 0; i < 4; i++)
                {

                    //Sending helloStrings for the device
                    message = helloString[i];       
                    sendMessage(message);

                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {

                    }
                }
            }
    }
    };
    output.start();

    // Initialize the BluetoothChatService to perform bluetooth connections
    mChatService = new BluetoothChatService(this, mHandler);


}

/**
     * Sends a message.
     * @param message  A string of text to send.
     */   
    private void sendMessage(String message) 
    {
        // TODO Auto-generated method stub
        // Check that we're actually connected before trying anything
        if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) 
        {
            Toast.makeText(this, R.string.not_connected, Toast.LENGTH_SHORT).show();           
            return;
        }

        byte[] send = message.getBytes();
        mChatService.write(send);

    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

春风十里 2025-01-13 17:31:04

您还需要在 run() 方法上声明@Override。

请参阅此问题的已接受答案 如何在 Android 中运行可运行线程?

You need to declare @Override on run() method too.

See the accepted answer to this question How to run a Runnable thread in Android?.

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