gpt4 book ai didi

android 无法在未调用 Looper.prepare 的线程内创建处理程序

转载 作者:行者123 更新时间:2023-11-30 03:20:54 25 4
gpt4 key购买 nike

尝试使用线程时出现此错误。

无法在尚未调用 Looper.prepare() 的线程内创建处理程序

    Thread anOpenConnectionThread = new Thread(new Runnable()
{
@Override
public void run()
{

try
{
openConnection();
}
catch (Exception err)
{
runOnUiThread(new Runnable() {
public void run() {

Toast.makeText(LaunchApp.this, "Opening connection with " + ip + " failed!", Toast.LENGTH_SHORT).show();
}
});

EneterTrace.error("Open connection failed.", err);
}
}
});

在onCreate中启动线程:

anOpenConnectionThread.start(); 

以防万一的打开连接代码:

private void openConnection() throws Exception
{
// Create sender sending MyRequest and as a response receiving MyResponse
IDuplexTypedMessagesFactory aSenderFactory = new DuplexTypedMessagesFactory();
mySender = aSenderFactory.createDuplexTypedMessageSender(MyResponse.class, MyRequest.class);

// Subscribe to receive response messages.
mySender.responseReceived().subscribe(myOnResponseHandler);

// Create TCP messaging for the communication.
ip = sProfile.getIp();
IMessagingSystemFactory aMessaging = new TcpMessagingSystemFactory();
IDuplexOutputChannel anOutputChannel
= aMessaging.createDuplexOutputChannel("tcp://" + ip + ":8060/");
Toast.makeText(this, ip, Toast.LENGTH_SHORT).show();

// Attach the output channel to the sender and be able to send
// messages and receive responses.
mySender.attachDuplexOutputChannel(anOutputChannel);
}

sProfile 已初始化!这是设置 ip 的地方(它在 toast 中显示了 ip,所以我 100% 确定它不为空)

        @Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int pos = profileSpinner.getSelectedItemPosition() + 1;
currentIp = myDb.getIp(pos);
Toast.makeText(SelectProfile.this, currentIp, Toast.LENGTH_SHORT).show();
}

getIp 正在返回那里设置的 ip。

public String getIp()
{
return currentIp;
}

最佳答案

在线程的运行方法中,您有 openConnection()。在 openConnection() 中,您有以下内容

   Toast.makeText(this, ip, Toast.LENGTH_SHORT).show();

无法从线程更新 ui。

关于android 无法在未调用 Looper.prepare 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19153892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com