我有以下Android代碼,其中 - Android應用程序聯繫Socket程序並在線程模型中獲取結果爲「SUCCESS」。之後,試圖顯示一個警告對話框,但Android程序得到異常作爲thread exiting with uncaught exception
我不能在這裏得到什麼錯誤。我不能在線程中顯示警報嗎?請指教。Android:Alert對話框拋出套接字線程中的異常
public class RandomIDActivity extends Activity {
.............
clientthread = new ClientThread();
Button connectBtn = (Button) findViewById(R.id.button2);
connectBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Thread t = new Thread(new ClientThread());
t.start();
}
});
}
class ClientThread implements Runnable {
@Override
public void run()
{
// We got this IP from servlet and stored temporarily, so retrieve it from there.
String socketServerIP = ((GlobalStore) RandomIDActivity.this.getApplication()).getSocketIPAddress();
Log.d("socketServerIP", socketServerIP);
try {
InetAddress serverAddr = InetAddress.getByName(socketServerIP);
socket = new Socket(serverAddr, SERVERPORT);
pw = new PrintWriter(socket.getOutputStream(), true);
EditText randTxtField = (EditText) findViewById(R.id.editText1);
pw.println(randTxtField.getText().toString());
//output.write(imgbyte,0,imgbyte.length);
pw.flush();
// Read randrom ID returned by Socket
BufferedReader socketReader;
try {
socketReader = new BufferedReader(new InputStreamReader(socket.getInputStream()));
msgStr = socketReader.readLine();
Log.d("msgStr: ", msgStr);
if (msgStr.equalsIgnoreCase("SUCCESS"))
{
socket.close();
// Crashing if I call alert dialog like this.
CobrowseAlertDialog("Successfully connected! Click OK to starts screen sharing!", true);
//Intent intent = new Intent(context, MainActivity.class);
//startActivity(intent);
}
else
{
CobrowseAlertDialog("There seems to be problem in connecting..Try connecting it again with proper Random Auth ID!", false);
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (UnknownHostException e1) {
e1.printStackTrace();
}
catch (IOException e1) {
e1.printStackTrace();
}
}
}
public void CobrowseAlertDialog(String msg, boolean bMove) {
AlertDialog.Builder builder = new AlertDialog.Builder(RandomIDActivity.this); // getParent()
builder.setTitle("Cobrowsing")
.setMessage(msg)
.setCancelable(false)
.setNegativeButton("Ok",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
//Intent intent = new Intent(context, MainActivity.class);
//startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
的logcat:
03-24 17:12:20.353: D/socketServerIP(32717): 192.168.1.21
03-24 17:12:20.503: D/msgStr:(32717): SUCCESS
03-24 17:12:29.952: W/dalvikvm(32717): threadid=17: thread exiting with uncaught exception (group=0x415efba8)
03-24 17:12:33.926: W/jdwp(32717): Debugger is telling the VM to exit with code=1
請登錄貓! –
只能在主線程上顯示警報對話框。在這裏發佈你的logcat –
更新了日誌貓。 – Stella