J'ai un mec de l'asyncaptage lorsque la fonction onpexecute est exécutée, cela me donne une exception
** java.lang.illegalstateException: vue com.android.internal.policy.impl.phonewindowrecorview@4440e20 a déjà été ajouté à la fenêtre gestionnaire. ** P> BlockQquote>
Lorsque la méthode Progressdialog's Show () est appelée. P>
Mon activité P>
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 } } }
7 Réponses :
Il y a un problème habituel avec ProgressDialogs et contextes, cela m'arrive tout le temps et il y a une section sur l'Android Doc pour ce problème exact. Vous l'avez probablement déclaré avec un contexte de "ceci" lorsque le contexte devrait être le nom de votre classe Java suivie de ". Ceci".
dialog = ProgressDialog.show(Example.this, "", "Doing stuff. Please wait...", true);
if ((numResults <= 0)) { Toast.makeText(TopNewsActivity.this, "No Result Found.",Toast.LENGTH_LONG).show(); finish(); } I believe this is not a good thing to do. Don't finish your activity from the non ui thread. Just return null.
N'appelez pas la finition () sur votre activité de DoIndbackground ()! Remettez NULL de cette méthode et appelez la finition () dans Onpostexecute ().
Essayez de supprimer le super.onpreexecute (); code> p>
L'exception a changé en: Invocation JDWP revenant avec sauf 0x444eA89F8 (Landroid / View / Windowmanager $ BadTokenException;)
Merci tout le monde mais j'ai compris quel était le problème, j'utilise Activitygroup, donc j'avais besoin de mettre :) p>
Essayez d'utiliser ce 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(); } } } }
S'il vous plaît, essayez d'expliquer les raisons pour lesquelles votre solution devrait fonctionner
Vous pouvez utiliser setProgressBainDeterminateVisibilité (true)
Prenez la boîte de dialogue code> code> par exemple.
Juge à partir du code source Android, regardons ce qui s'est passé lorsque vous avez appelé la méthode Numéro de dialogue Afficher () Code> Méthode. Le code peut être simplifié comme ci-dessous: p>
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â main, me.shouheng.suix.SampleApp$customCrash$1.onCrash(SampleApp.kt:64)
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
â ************* uncaught exception *************
â Time Of Crash : 2020-01-10 14-46-43
â Device Manufacturer: OnePlus
â Device Model : ONEPLUS A6000
â Android Version : 9
â Android SDK : 28
â App VersionName : 1.0
â App VersionCode : 1
â
â java.lang.IllegalStateException: View DecorView@3a84c11[MainActivity] has already been added to the window manager.
â at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:328)
â at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
â at android.app.Dialog.show(Dialog.java:329)
â at me.shouheng.suix.MainActivity$doCreateView$7.onClick(MainActivity.kt:72)
â at android.view.View.performClick(View.java:6669)
â at android.view.View.performClickInternal(View.java:6638)
â at android.view.View.access$3100(View.java:789)
â at android.view.View$PerformClick.run(View.java:26145)
â at android.os.Handler.handleCallback(Handler.java:873)
â at android.os.Handler.dispatchMessage(Handler.java:99)
â at android.os.Looper.loop(Looper.java:193)
â at android.app.ActivityThread.main(ActivityThread.java:6898)
â at java.lang.reflect.Method.invoke(Native Method)
â at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537)
â at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
âââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââââ
@Vladimir Ivanov: ont ajouté le code s'il vous plaît jeter un oeil