簡體   English   中英

線程退出與未捕獲的異常android

[英]Thread Exiting with uncaught exception android

我正在嘗試做一個Android應用程序,我們必須將變量發布到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

有時,您可能無法使用Try-Catch捕獲異常(嗯,您可以用Google搜索原因)。但是在這里,我建議您一種解決您的情況的方法。Thread類具有一個函數setDefaultUncaughtExceptionHandler(Thread.UncaughtExceptionHandler eh)要處理。未捕獲的異常。

1使您的Activity實現UncaughtExceptionHandler

公共類MainActivity擴展Activity實現UncaughtExceptionHandler {

....

}

2讓您的Activity覆蓋uncaughtException

public void uncaughtException(Thread arg0,Throwable arg1){

    //here to process the uncaught exception

}

3在活動的OnCreate中

只需調用Thread.setDefaultUncaughtExceptionHandler(this); 讓它起作用。

我希望這可以有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM