嘗試使用線程時出現此錯誤。android無法在線程中創建處理程序,但尚未調用Looper.prepare
Can't create handler inside thread that has not called 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被設置在哪裏(它顯示了土司的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;
}
什麼是EneterTrace? –