java.lang.RuntimeException:无法在线程内创建处理程序
我收到错误...无法在未调用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还需要在 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?.