2011-04-29 82 views
6

我有一個的AsyncTask時onPreExecute函數執行它給了我一個異常的AsyncTask onPreExecute progressdialog

** java.lang.IllegalStateException:查看 [email protected] 已經被添加到窗口 經理。**

時progressDialog的show()方法被調用。

我的動態

public class TopNewsActivity extends ListActivity { 

public static final String LOG_TAG = "Infra"; 
private ProgressDialog progressDialog; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listplaceholder); 
    new BackgroundAsyncTask().execute(); 
} 

public class BackgroundAsyncTask extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>>> { 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 
     progressDialog = new ProgressDialog(TopNewsActivity.this); 
     progressDialog.setCancelable(true); 
     progressDialog.setMessage("Loading..."); 
     progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
     progressDialog.setProgress(0); 
     progressDialog.show(); 
    } 

    @Override 
    protected ArrayList<HashMap<String, String>> doInBackground(String... paths) { 
     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 

     String xml = XMLfunctions.getTopNewsXML(); 
     Document doc = XMLfunctions.XMLfromString(xml); 

     int numResults = XMLfunctions.numResults(doc); 
     Log.d(LOG_TAG, "Number of Results: " + numResults); 
     if ((numResults <= 0)) { 
      Toast.makeText(TopNewsActivity.this, "No Result Found",Toast.LENGTH_LONG).show(); 
      finish(); 
     } 

     NodeList nodes = doc.getElementsByTagName("result"); 

     for (int i = 0; i < nodes.getLength(); i++) { 
      HashMap<String, String> map = new HashMap<String, String>(); 

      Element e = (Element) nodes.item(i); 
      map.put("id", XMLfunctions.getValue(e, "id")); 
      map.put("title", XMLfunctions.getValue(e, "title")); 
      mylist.add(map); 
     } 
     return mylist; 
    } 

    @Override 
    protected void onProgressUpdate(Integer... values) { 
     super.onProgressUpdate(values); 
    } 

    protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
     ListAdapter adapter = new SimpleAdapter(TopNewsActivity.this, result, R.layout.list_item, new String[] { "title" }, new int[] { R.id.item_title }); 
     setListAdapter(adapter); 
     progressDialog.dismiss(); 

     final ListView lv = getListView(); 

     lv.setTextFilterEnabled(true); 
     lv.setOnItemClickListener(new OnItemClickListener() { 
      @SuppressWarnings("unchecked") 
      @Override 
      public void onItemClick(AdapterView<?> a, View view, final int position, long id) { 

        HashMap<String, String> o = (HashMap<String, String>) lv.getItemAtPosition(position); 

        Intent i = new Intent(TopNewsActivity.this, NewsDetails.class); 
        i.putExtra("content_id", o.get("id")); 
        i.putExtra("title", o.get("title")); 
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

        View v = TopNewsGroup.group.getLocalActivityManager().startActivity("ShowNews", i).getDecorView(); 

        // Again, replace the view 
        TopNewsGroup.group.setContentView(v); 

      } 
     }); 

    } 

} 

public class MySimpleAdapter extends SimpleAdapter { 
    public MySimpleAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to) { 
     super(context, data, resource, from, to); 
     // TODO Auto-generated constructor stub 
} 

} 
} 

請幫助!!!!!

+0

顯示代碼,請。 – 2011-04-29 13:43:14

+0

@Vladimir Ivanov:已添加驗證碼請看看 – ReNa 2011-04-29 13:46:18

回答

1

謝謝大家,但我已經想通了什麼問題,我使用的ActivityGroup所以我需要把progressDialog =新ProgressDialog(TopNewsGroup.group);這個解決我的問題

:)

4

進度對話框和上下文有一個常見問題,它總是發生在我身上,並且在這個確切的問題上有一個關於android文檔的部分。當上下文實際上應該是您的Java類名稱後跟「.this」時,您可能已經用「this」的上下文聲明瞭它。

dialog = ProgressDialog.show(Example.this, "", 
       "Doing stuff. Please wait...", true); 

這是因爲您希望progressDialog顯示在主類中,而不是在Async類中。

如果這不能解決它,你需要發佈代碼。

+0

此解決方案無效。我已經發布了我的代碼。謝謝 – ReNa 2011-04-29 13:58:44

2
if ((numResults <= 0)) { 
    Toast.makeText(TopNewsActivity.this, "No Result Found.",Toast.LENGTH_LONG).show(); 
    finish(); 
} 

我相信這不是一件好事。不要從非用戶界面完成你的活動。只需返回null。

+0

我沒有明白你的意思。你能解釋一下嗎 – ReNa 2011-04-29 13:52:32

+3

不要在doInBackground()的活動中調用finish()!從此方法返回null,並在onPostExecute()中調用finish()。 – 2011-04-29 14:04:37

1

嘗試取出super.onPreExecute();

+0

該異常已更改爲:使用exceptObj = 0x44ea89f8返回的JDWP調用(Landroid/view/WindowManager $ BadTokenException;) – ReNa 2011-04-29 14:09:33

0

嘗試使用此代碼

import java.util.ArrayList; 
import java.util.HashMap; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.util.EntityUtils; 
import org.json.JSONArray; 
import org.json.JSONObject; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.AdapterView; 
import android.widget.AdapterView.OnItemClickListener; 
import android.widget.ListView; 
import android.widget.Toast; 

import com.example.tabs.R; 
import com.s3.daycare.adapters.CalendarAdapter; 
import com.s3.daycare.description.CalendarDescription; 

public class Calendar extends Activity { 

    String url = "http://mobile.s3technology.net/DayCare/webservices/Get_calender.php?"; 

    GetData data; 

    ProgressDialog progressDialog; 

    ListView list_of_calendar; 

    ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.calendar); 

     list_of_calendar = (ListView) findViewById(R.id.list_of_calendar); 

     new GetData().execute(); 

     //ListView listView = getListView(); 
     list_of_calendar.setOnItemClickListener(new OnItemClickListener() { 

      @Override 
      public void onItemClick(AdapterView<?> parent, View view, 
        int position, long id) 
      { 
       HashMap<String, String> map = list.get(position);    

       Intent intent = new Intent(Calendar.this, CalendarDescription.class); 

       intent.putExtra("name", map.get("name")); 

       intent.putExtra("date", map.get("date")); 

       intent.putExtra("description", map.get("description")); 

       startActivity(intent); 


      } 

     }); 
    } 

    private class GetData extends AsyncTask<String, Void, JSONObject> { 

     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 

      progressDialog = ProgressDialog.show(Calendar.this, 
        "", ""); 

     } 

     @Override 
     protected JSONObject doInBackground(String... params) { 

      String response; 

      try { 

       HttpClient httpclient = new DefaultHttpClient(); 

       HttpPost httppost = new HttpPost(url); 

       HttpResponse responce = httpclient.execute(httppost); 

       HttpEntity httpEntity = responce.getEntity(); 

       response = EntityUtils.toString(httpEntity); 

       Log.d("response is", response); 

       return new JSONObject(response); 

      } catch (Exception ex) { 

       ex.printStackTrace(); 

      } 

      return null; 
     } 

     @Override 
     protected void onPostExecute(JSONObject result) 
     { 
      super.onPostExecute(result); 

      progressDialog.dismiss(); 

      if(result != null) 
      { 
       try 
       { 
        JSONObject jobj = result.getJSONObject("result"); 

        String status = jobj.getString("status"); 

        if(status.equals("true")) 
        { 
         JSONArray array = jobj.getJSONArray("data"); 

         for(int x = 0; x < array.length(); x++) 
         { 
          HashMap<String, String> map = new HashMap<String, String>(); 

          map.put("name", array.getJSONObject(x).getString("name")); 

          map.put("date", array.getJSONObject(x).getString("date")); 

          map.put("description", array.getJSONObject(x).getString("description")); 

          list.add(map); 
         } 

         CalendarAdapter adapter = new CalendarAdapter(Calendar.this, list); 

         list_of_calendar.setAdapter(adapter); 
        } 
       } 
       catch (Exception e) 
       { 
        e.printStackTrace(); 
       } 
      } 
      else 
      { 
       Toast.makeText(Calendar.this, "Network Problem", Toast.LENGTH_LONG).show(); 
      } 
     } 

    } 
} 
+0

請嘗試解釋您的解決方案暫停工作的原因 – 2014-04-18 10:01:03

0

您可以使用setProgressBaIndeterminateVisibility(真)

@Override 
    protected void onPreExecute() { 
     setProgressBarIndeterminateVisibility(true); 
    }