2013-07-11 89 views
0

我使用AsyncTask從我的服務器獲取字符串。AsyncTask完成後主線程崩潰

我的問題是的AsyncTask結束後我的主線程崩潰和應用程序是

裝上一個窗口。

奇怪的是,我沒有得到任何錯誤代碼在我的logcat。

感謝您的幫助。

這是我的代碼和logcat的:

07-12 00:58:51.162: I/ActivityManager(1834): START u0 {cmp=com.tomer.workoutlog/com.example.workoutlog.AddWorkOutPage (has extras)} from pid 28701 
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): do_aic3254_control device: 1 mode: 0 record: 0 
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: new_aic_rxmode 13 cur_aic_rx 29 
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl() 
07-12 00:58:51.182: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: try ioctl 0x40047313 with arg 13 
07-12 00:58:51.192: D/AudioHardwareMSM7X30(1489): aic3254_ioctl: new_aic_txmode 29 cur_aic_tx 29 
07-12 00:58:51.192: D/AudioHardwareMSM7X30(1489): value of device and enable is 6 1 ALSA dev id:6 
07-12 00:58:51.232: D/AudioHardwareMSM7X30(1489): updateACDB: (11, 6, 0, 607) 
07-12 00:58:51.232: I/HTC Acoustic(1489): update ACDB id: (tx, rx, tx_acdb, rx_acdb) = (11, 6, 0, 607) 
07-12 00:58:51.232: D/AudioHardwareMSM7X30(1489): msm_route_stream(PCM_PLAY,5,6,1) 
07-12 00:58:51.652: I/ActivityManager(1834): Displayed com.tomer.workoutlog/com.example.workoutlog.AddWorkOutPage: +474ms 
07-12 00:58:52.033: W/InputMethodManagerService(1834): Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 

代碼:

class GetKey extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      pDialog = new ProgressDialog(AddWorkOutPage.this); 
      pDialog.setMessage("loading..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 

     /** 
     * Creating product 
     * */ 
     protected String doInBackground(String... args) { 

       JSONParser jsonParser = new JSONParser(); 
       JSONObject json = null; 
       // Building Parameters 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 

       params.add(new BasicNameValuePair("exercise", exercise)); 

       json = jsonParser.makeHttpRequest(url_get_key,"GET", params); 

       try { 
        base64EncodedPublicKey = json.getString("key"); 
       } catch (JSONException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 

      // check for success tag 
      try { 
       int success = json.getInt(TAG_SUCCESS); 

       if (success == 1) { 
        // successfully created product 

        //Log.d("ok", json.toString()); 
        // closing this screen 
        finish(); 
       } else { 
        // failed to create product 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialog 
     * **/ 
     protected void onPostExecute(String file_url) { 
      pDialog.dismiss(); 

     } 

    } 
+0

爲什麼要告訴'onPostExecute()'接受'String',但是從'doInBackground()'返回'null'? – codeMagic

+0

你怎麼知道你正在崩潰?堆棧跟蹤或其他任何指示崩潰的消息是否有異常? – Christos

+0

我可以刪除字符串,你認爲這就是解決問題?我知道它崩潰,因爲我在我的設備上看到它 – dasdasd

回答

1

兩個潛在的問題:

  1. 正如我的評論,你告訴onPostExecute()接受String,但返回nulldoInBackground()。我不確定這會導致問題,但我從來沒有嘗試過。如果你不想返回任何東西可以改變

    保護無效onPostExecute(無效響應)

,改變你的類聲明

class GetKey extends AsyncTask<String, Void, Void> { 

假設你傳遞一個String在​​當您撥打任務

2您正在呼叫finish()doInBackground()AsyncTask沒有完成()方法,因此您要麼完成Activity,要麼在那裏出錯。

+0

謝謝。我刪除完成並更改爲AsyncTask ,它工作 – dasdasd

+0

太好了,我很高興它的工作! – codeMagic

+0

順便說一句,在返回null不是問題(但它是無用的)。 :) – Enrichman

0

問題是你的doInBackground()方法調用finish(),所以你以某種方式修改你的上下文並得到異常。

而不是一個字符串從結果返回整數,並在onPostExecute()完成()您的活動。

class GetKey extends AsyncTask<String, String, Integer> { 
    /** 
    * Before starting background thread Show Progress Dialog 
    * */ 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     pDialog = new ProgressDialog(AddWorkOutPage.this); 
     pDialog.setMessage("loading..."); 
     pDialog.setIndeterminate(false); 
     pDialog.setCancelable(true); 
     pDialog.show(); 
    } 

    /** 
    * Creating product 
    * */ 
    protected Integer doInBackground(String... args) { 
     JSONParser jsonParser = new JSONParser(); 
     JSONObject json = null; 
     // Building Parameters 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 

     params.add(new BasicNameValuePair("exercise", exercise)); 

     json = jsonParser.makeHttpRequest(url_get_key,"GET", params); 

     try { 
      base64EncodedPublicKey = json.getString("key"); 
     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 

     // check for success tag 
     int success = 0; 
     try { 
      success = json.getInt(TAG_SUCCESS);     
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return success; 
    } 

    /** 
    * After completing background task Dismiss the progress dialog 
    * **/ 
    protected void onPostExecute(Integer success) { 
     pDialog.dismiss(); 
     if (success == 1) { 
      // successfully created product 
      //Log.d("ok", json.toString()); 
      // closing this screen 
      finish(); 
     } else { 
      // failed to create product 
     } 
    } 

}