Android - 致命异常AsyncTask#1

时间:2015-03-07 13:21:08

标签: android

我有以下代码,我在doInBakground任务中遇到问题,这段代码工作一次,在重启设备后运行应用程序的所有尝试都崩溃了

public class DBConnection extends AsyncTask<String,Void,String> {

private Context context ;
static JSONArray jArray = null;
String title, location, desc, time;

public DBConnection(Context context) {
      this.context = context;

   }

public void print(String msg){
    Toast.makeText(this.context, msg ,Toast.LENGTH_LONG).show();
}


@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    try{
        String link="http://10.0.3.2/friendLocation.php";

         URL url = new URL(link);
         URLConnection conn = url.openConnection(); 
         conn.setDoOutput(true); 

         BufferedReader reader = new BufferedReader 
                (new InputStreamReader(conn.getInputStream()));
         StringBuilder sb = new StringBuilder();
         String line = null;
         // Read Server Response
         while((line = reader.readLine()) != null){
                   sb.append(line);
                   break;
                }

            return sb.toString();

    }catch(Exception e){
        return new String("Exception: " + e.getMessage());
     }
}

 @Override
   protected void onPostExecute(String result) {

          try{
               jArray = new JSONArray(result);

               sendEvent();
               }
               catch(JSONException e1){
                Toast.makeText(this.context, "No Data Found" ,Toast.LENGTH_LONG).show();
               } catch (ParseException e1) {
            e1.printStackTrace();
          }

   }


 public void sendEvent(){
     Intent intent = new Intent(this.context, Event.class); 
     this.context.startActivity(intent);
 }


}

任何人都可以帮忙吗? 提前谢谢。

堆栈追踪:

03-07 13:26:39.224: D/OpenGLRenderer(1351): Enabling debug mode 0
03-07 13:26:39.252: I/Choreographer(1351): Skipped 471 frames!  The     application may be doing too much work on its main thread.
03-07 13:26:42.256: W/dalvikvm(1351): threadid=11: thread exiting with uncaught exception (group=0xa4cdfb20)
03-07 13:26:42.264: E/AndroidRuntime(1351): FATAL EXCEPTION: AsyncTask #1
03-07 13:26:42.264: E/AndroidRuntime(1351): Process: com.example.calendar, PID: 1351
03-07 13:26:42.264: E/AndroidRuntime(1351): java.lang.RuntimeException: An     error occured while executing doInBackground()
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at java.lang.Thread.run(Thread.java:841)
03-07 13:26:42.264: E/AndroidRuntime(1351): Caused by:   java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.os.Handler.<init>(Handler.java:200)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.os.Handler.<init>(Handler.java:114)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.widget.Toast$TN.<init>(Toast.java:327)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.widget.Toast.<init>(Toast.java:92)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.widget.Toast.makeText(Toast.java:241)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at com.example.calendar.DBConnection.print(DBConnection.java:29)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at com.example.calendar.DBConnection.doInBackground(DBConnection.java:34)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at com.example.calendar.DBConnection.doInBackground(DBConnection.java:1)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-07 13:26:42.264: E/AndroidRuntime(1351):     at      java.util.concurrent.FutureTask.run(FutureTask.java:237)
    03-07 13:26:42.264: E/AndroidRuntime(1351):     ... 4 more

1 个答案:

答案 0 :(得分:0)

您无法在后台主题中显示Toast。请从Toast方法中删除print()。如果您使用此Toast进行调试,请考虑切换到Log类以将内容记录到LogCat,或在IDE中使用断点。