AsyncTask onPreExecute progressdialog

I have AsyncTask when the onPreExecute function is executed, it gives me an exception

** java.lang.IllegalStateException: The com.android.internal.policy.impl.PhoneWindow$DecorView@44ea0e20 view has already been added to the manager window. **

when the progressDialog show () method is called.

My activity

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 } } } 

Please, help!!!!!

+6
source share
6 answers

Thanks to everyone, but I found out what the problem is: I use ActivityGroup, so I had to put progressDialog = new ProgressDialog (TopNewsGroup.group); this solved my problem p>

:)

+1
source

There is a common problem with progressdialogs and contexts, this happens to me all the time, and there is a section for the android document for this exact problem. You probably declared it with the "this" context, when the context should actually be the name of your Java class, followed by ".this".

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

This is because you want progressDialog to appear in the main class, and not in the Async class.

If this does not solve the problem, you will need to publish the code.

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

I think this is not very good. Do not terminate your activity from a non ui thread. Just return null.

+2
source

Try removing super.onPreExecute();

+1
source

try using this code

 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
source

You can use setProgressBaIndeterminateVisibility (true)

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

Source: https://habr.com/ru/post/886961/


All Articles