线程退出未捕获的异常android

时间:2015-11-09 06:51:39

标签: android thread-safety

我正在尝试做一个Android应用程序,我们必须将变量POST到php页面上。 我几乎完成但是当控件进入线程时应用程序崩溃..错误是threadid = 12:线程退出未捕获的异常 任何人都可以请我解决这个问题...谢谢。 我正在粘贴我的代码和logcat ......

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;


public class MainActivity extends Activity {

TextView content;
EditText latitude, longitude, rad;
String lat, longi, radius, Pass; 
String data,text;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    content    =   (TextView)findViewById( R.id.content);
    latitude      =   (EditText)findViewById(R.id.lat);
    longitude      =   (EditText)findViewById(R.id.longi);
    rad      =    (EditText)findViewById(R.id.radius);



    Button saveme=(Button)findViewById(R.id.save);

    saveme.setOnClickListener(new Button.OnClickListener(){

        public void onClick(View v)
        {
            try{

                     // CALL GetText method to make post method call
                    GetText();
             }
            catch(Exception ex)
             {
                content.setText(" url exeption! " );
             }
        }
    });  
}

 // Create GetText Metod
 public  void  GetText()  throws  UnsupportedEncodingException
{

try{
    // Get user defined values
    lat = latitude.getText().toString();
    longi   = longitude.getText().toString();
    radius   = rad.getText().toString();


     // Create data variable for sent values to server  

       data = URLEncoder.encode("lat", "UTF-8") 
                   + "=" + URLEncoder.encode(lat, "UTF-8"); 

      data += "&" + URLEncoder.encode("longi", "UTF-8") + "="
                  + URLEncoder.encode(longi, "UTF-8"); 

      data += "&" + URLEncoder.encode("radius", "UTF-8") 
                  + "=" + URLEncoder.encode(radius, "UTF-8");


       text = "";


      // Send data 


         new AsyncCaller().execute();
}
catch(Exception e)
{
    e.printStackTrace();
}
}





 class AsyncCaller extends AsyncTask<Void, Void, Void>
 {


 BufferedReader reader=null;
 @Override
 protected void onPreExecute() {
 super.onPreExecute();

//this method will be running on UI thread

 }
 @Override
 protected Void doInBackground(Void... params) {


try {
  //Create connection

    // Defined URL  where to send data
    URL url = new URL("http://dev.pioneercodes.com/busapi/getBusStops.php");

 // Send POST data request

  URLConnection conn = url.openConnection(); 
  conn.setDoOutput(true); 
  OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream()); 
  wr.write(data); 
  wr.flush(); 

  // Get the server response 
  reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
  StringBuilder sb = new StringBuilder();
  String line = null;
  text = sb.toString();

  // Read Server Response
  while((line = reader.readLine()) != null)
      {
             // Append server response in string
             sb.append(line + "\n");
      }


} catch(Exception ex)
{
    ex.printStackTrace();
 }
   finally
  {
   try
   {

      reader.close();
   }

   catch(Exception ex) {}
  }

 // Show response on activity
 content.setText( text  );
return null;
 }

@Override
 protected void onPostExecute(Void result) {
  super.onPostExecute(result);

content.setText("sdfsd");

 }


 }

 }

logcat的

    11-09 12:08:44.667: I/PGA(28018): Attempting to create new SOCKET       connectionn pid = 28018, tid = 28018
    11-09 12:08:44.697: I/PGA(28018): New SOCKET connection: com.example.newcon (pid 28018, tid 28018)
    11-09 12:08:44.697: W/PGA(28018): [28018] egl: eglCreateWindowSurface (0x557957a0, 0x0, 0x7976c808, 0x7757f0e0)
    11-09 12:08:44.697: W/PGA(28018): [28018] egl: eglCreateWindowSurface (0x557957a0, 0x0, 0x7976c808, 0x7757f0e0) returned
    11-09 12:08:44.707: D/OpenGLRenderer(28018): Enabling debug mode 0
    11-09 12:08:53.457: D/dalvikvm(28018): GC_FOR_ALLOC freed 351K, 19% free 3040K/3720K, paused 0ms, total 0ms
    11-09 12:08:53.777: W/dalvikvm(28018): threadid=12: thread exiting with uncaught exception (group=0x64cfdb20)
    11-09 12:08:53.777: I/Process(28018): Sending signal. PID: 28018 SIG: 9

1 个答案:

答案 0 :(得分:1)

有时,你可能无法通过Try-Catch捕获异常(呃,你可能会谷歌的原因)。但是在这里我会建议你一种方法可以解决你的情况.Thread类有一个函数setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh处理未捕获的异常。

1让您的Activity实现UncaughtExceptionHandler

public class MainActivity extends Activity实现UncaughtExceptionHandler {

...

}

2让你的Activity覆盖uncaughtException

public void uncaughtException(Thread arg0,Throwable arg1){

    //here to process the uncaught exception

}

3在您的活动的OnCreate

只需调用Thread.setDefaultUncaughtExceptionHandler(this);让它运作。

我希望这可以提供一些帮助。