2014-03-24 38 views
0

我有以下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 
+0

請登錄貓! –

+0

只能在主線程上顯示警報對話框。在這裏發佈你的logcat –

+0

更新了日誌貓。 – Stella

回答

0

在Android中,線程是不能直接更新UI線程。如果你想更新你的UI,你應該使用

Handler。

Handler mHandler; 

    @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); 

    //add handler here 
    mHandler.post(new Runnable{ 
public void run(){ 
    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(); 
     } 

    } 
} 
+0

即使在添加這樣的處理程序代碼後也觀察到同樣的問題。 – Stella

+0

嗨,它很好用!謝謝。在「CobrowseAlertDialog」方法中,當我使用this-> Intent intent = new Intent(context,MainActivity.class)添加移動到下一個視圖的代碼時, startActivity(intent);拋出致命的異常作爲線程退出與未捕獲的異常。可能是什麼原因?請指教。 – Stella

+0

我認爲上下文不應該用在alertbox上。例如(){Intent intent = new Intent(context,MainActivity.class);方法如下: –

0

後臺線程不能彈出對話框。你可以做的是,你可以在UI線程上使用一些Handler來彈出對話框外觀。

別的一個選擇是也可以,但不是那麼好,不是很大,以recommened

runOnUiThread(new Runnable(){ 
public void run(){ 
////code for alert dialog 
} 
}); 
0

在android中,線程不是直接更新UI線程。如果妳想要更新烏爾UI,U應使用

類ClientThread實現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(); 
     } 

    } 
} 

處理程序處理程序=新的處理程序();

Button connectBtn =(Button)findViewById(R.id.button2); connectBtn.setOnClickListener(新View.OnClickListener(){

  @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

       handler.post(new 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(); 
     } 

    } 
} 

); } });

+0

它不工作,它在這條線本身的例外失敗,socket = new Socket(serverAddr,SERVERPORT); – Stella

+0

它工作,如果我使用處理程序。 – Stella

+0

但是,在同一個流程中的另一個問題,在「CobrowseAlertDialog」方法中,當我使用this-> Intent intent = new Intent(context,MainActivity.class)添加移動到下一個視圖的代碼時; startActivity(intent);拋出致命的異常作爲線程退出與未捕獲的異常。可能是什麼原因?請指教。 – Stella