Android:Alert对话框在套接字线程内抛出异常

时间:2014-03-24 11:38:16

标签: android

我有以下Android代码,其中Android应用程序联系Socket程序并在线程模型中获得结果为“SUCCESS”。之后,尝试显示警告对话框,但android程序获取异常为thread exiting with uncaught exception 我不知道这里有什么问题。我不能在线程中显示警报吗?请指教。

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

3 个答案:

答案 0 :(得分:0)

后台线程无法弹出对话框。您可以做的是,您可以在UI线程上使用一些处理程序来弹出对话框外观。

其他一种替代方案也可用,但不太好,推荐不太好

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

答案 1 :(得分:0)

在android中,Thread并不直接更新UI线程。如果你想更新你的UI,你应该使用

处理程序。

 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();
        }

     }
}

答案 2 :(得分:0)

在android中,Thread并不直接更新UI线程。如果你想更新你的UI,你应该使用

类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();
        }

     }
}

Handler handler = new Handler();

按钮connectBtn =(按钮)findViewById(R.id.button2);             connectBtn.setOnClickListener(new 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();
        }

     }
}

);                 }             });