2013-07-26 149 views
0

onPostExecute有什麼問題。它顯示了該方法中的錯誤。適用於ProgressDialog。 json和數據庫處理程序都正常工作。 ProgressDialog已經完成了同樣的工作。android asynctask中的自定義對話框

class ProcessBalance extends AsyncTask<String, Void, List<String>> { 

     private Dialog dialog; 

     /** 
     * Before starting background thread 
     * Show Progress Bar Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 

      final Dialog dialog = new Dialog(UserhomeActivity.this); 
      dialog.setTitle("test"); 
      dialog.setContentView(R.layout.dialoglayout); 
      dialog.getWindow().getAttributes().windowAnimations = R.style.DialogAnimation; 
      Button btnDismiss = (Button)dialog.getWindow().findViewById(R.id.dismiss); 
      // TextView balview = (TextView) dialog.findViewById(R.id.textView1); 
     //  balview.setText("Please wait for the balance."); 

      //http://www.mkyong.com/android/android-custom-dialog-example/ 

      btnDismiss.setOnClickListener(new OnClickListener(){ 

       public void onClick(View v) { 
        dialog.dismiss(); 
       }}); 

      dialog.show(); 
     } 

     /** 
     * Downloading file in background thread 
     * */ 
     @Override 
     protected List<String> doInBackground(String... args0) { 
     // DatabaseHandler db = new DatabaseHandler(getApplicationContext()); 
      //HashMap<String, String> user = db.getUserDetails(); 
      UserFunctions userFunction = new UserFunctions(); 
      //JSONObject json = userFunction.getBalance(user.get("mobile_no")); 
      JSONObject json = userFunction.getBalance("1234567890"); 
      List<String> LIST = new ArrayList<String>(); 
      try { 
       if (json.getString(KEY_SUCCESS) != null) { 
        String res = json.getString(KEY_SUCCESS); 
        LIST.add(json.getString("success")); 
        LIST.add(json.getString("balance")); 
      } 

      } catch (NullPointerException e) { 
       e.printStackTrace(); 

      } 
      catch (JSONException e) { 
       e.printStackTrace(); 
      } 

      return LIST; 
     } 


     /** 
     * After completing background task 
     * Dismiss the progress dialog 
     * **/ 

     protected void onPostExecute(List<String> result) { 

      String responseCodestr = result.get(0); 
      final int responseCode = Integer.parseInt(responseCodestr); 
      String bal = result.get(1); 
      TextView balview = (TextView) dialog.findViewById(R.id.textView1); 

      if(responseCode==1){ 
       balview.setText("test "+bal); 
      } else { 
        balview.setText("test2"); 
      } 

     } 

     } 
+1

什麼錯誤? –

+0

錯誤是什麼?如果需要提供堆棧跟蹤。 – Suji

+0

當你得到'TextView balview =(TextView)dialog.findViewById(R.id.textView1);'in'onPreExecute'然後嘗試在'onPostExecute'中設置文本時會發生什麼? (Button)dialog.findViewById(R.id.dismiss);' – Rajeev

回答

1

您有兩個對話框,一個作爲成員,另一個作爲最後一個在onPreExecute中定義。您應該更改下面的代碼

private Dialog dialog; 
    @Override 
    protected void onPreExecute() { 

     final Dialog dialog = new Dialog(UserhomeActivity.this); 

此:

private final Dialog dialog; 
    @Override 
    protected void onPreExecute() { 

     this.dialog = new Dialog(UserhomeActivity.this); 
+0

好的!感謝所有特別AitorTheRed :)它的工作如你所說。 – user2534310

0

的問題是你聲明對話框兩次。

一是onPreExcecute

private Dialog dialog; 

一個是內部onPreExcecute

final Dialog dialog = new Dialog(UserhomeActivity.this); 

要初始化的對話是第二個,你試圖在onPostExceute使用對話框是第一位的。

解決方案是

變化

final Dialog dialog = new Dialog(UserhomeActivity.this); 

dialog = new Dialog(UserhomeActivity.this);