2012-04-05 150 views
0

在我的應用程序中,我需要調用一個函數(更新textviews的values),我需要在按下OK按鈕時在AlertDialg中調用此函數。

問題是我怎麼可以在按下對話框的按鈕之後調用RefreshData.execute()?
這是一個錯誤: android.view.WindowManager $ BadTokenException:無法添加窗口 - 標記null不適用於應用程序。

代碼:

public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.details); 

     Bundle extras = getIntent().getExtras(); 

     if (extras != null) { 
      x = extras.getString("key").toString(); 
     } else { 

      Toast.makeText(getBaseContext(), "null", 0).show(); 
     } 

     tv_summary = (TextView) findViewById(R.id.tv_summary); 
     tv_servings_result = (TextView) findViewById(R.id.tv_servings_result); 
     tv_calories_result = (TextView) findViewById(R.id.tv_calories_result); 
     tv_fat = (TextView) findViewById(R.id.tv_fat); 
     tv_monofat = (TextView) findViewById(R.id.tv_monofat); 
     tv_satfat = (TextView) findViewById(R.id.tv_satfat); 
     tv_ch = (TextView) findViewById(R.id.tv_ch); 
     tv_sug = (TextView) findViewById(R.id.tv_sug); 

     new LoadDetails().execute(); 

     Button MealSize = (Button) findViewById(R.id.btn_size); 

     MealSize.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 
       show(); 

       Toast.makeText(getBaseContext(), F + "", 0).show(); 
      } 

     }); 

    } 

    void Refresh() { 
     new RefreshData().execute(); 
    } 

    void show() { 

     AlertDialog.Builder alert = new AlertDialog.Builder(this); 
     alert.setTitle("Title"); 
     alert.setMessage("Message"); 

     final EditText input = new EditText(this); 
     alert.setView(input); 
     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int whichButton) { 

       F = Float.parseFloat(input.getText().toString()); 
       new RefreshData().execute(); 

      } 
     }); 

     alert.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int whichButton) { 
         // Canceled. 
        } 
       }); 

     alert.show(); 

    } 

    private class RefreshData extends AsyncTask<Void, Void, Void> { 

     private ProgressDialog progressDialog; 

     @Override 
     protected void onPreExecute() { 
      this.progressDialog = ProgressDialog.show(getBaseContext(), "", 
        " Loading..."); 
     } 

     @Override 
     protected void onPostExecute(final Void unused) { 

      this.progressDialog.dismiss(); 

      try { 
       this.progressDialog.dismiss(); 
       tv_servings_result.setText(servings_result + "" + F); 
       tv_calories_result.setText(cal + "g"); 
       tv_fat.setText(ff + ""); 
       tv_monofat.setText(mm + ""); 
       tv_satfat.setText(sasa + ""); 
       tv_ch.setText(chch + ""); 
       tv_sug.setText(sugar + ""); 

      } catch (Exception e) { 
       Log.e("log_tag", 
         "Eraaaaaaaaaaaaaaaaaaa connection" + e.toString()); 

      } 

     } 

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

      try { 
       sugar = Float.valueOf(sug).floatValue(); 
       sugar *= F; 

       cal = Float.valueOf(calories_result).floatValue(); 
       cal *= F; 

       ff = Float.valueOf(fat).floatValue(); 
       ff *= F; 

       mm = Float.valueOf(monofat).floatValue(); 
       mm *= F; 

      } catch (Exception e) { 
       Log.e("log_tag", 
         "Eraaaaaaaaaaaaaaaaaaa connection" + e.toString()); 

      } 

      return null; 

     } 

    } 

}

回答

2

而是採用

this.progressDialog = ProgressDialog.show(getBaseContext(), ""," Loading..."); 

嘗試

this.progressDialog = ProgressDialog.show(yourActivity.this, ""," Loading..."); 
+0

究竟在何處是你的錯誤來了嗎? line no – 2012-04-05 16:46:35

+0

你的回答是正確的。謝謝你。 – user2012 2012-04-05 16:50:37

相關問題