[英]Activity has leaked window com.android.internal.policy.impl.PhoneWindows$DecorView{42d9a800
im writting an app that gets data from a database however im getting this error: 我正在编写一个从数据库获取数据的应用程序,但是我正在收到此错误:
02-10 11:37:08.779 19458-19458/com.familiestvw.whatson E/WindowManager﹕ Activity com.familiestvw.whatson.AllVenuesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42d9a800 V.E..... R......D 0,0-1026,288} that was originally added here
android.view.WindowLeaked: Activity com.familiestvw.whatson.AllVenuesActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42d9a800 V.E..... R......D 0,0-1026,288} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:450)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:258)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
at android.app.Dialog.show(Dialog.java:287)
at com.familiestvw.whatson.AllVenuesActivity$LoadAllVenues.onPreExecute(AllVenuesActivity.java:74)
at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
at android.os.AsyncTask.execute(AsyncTask.java:534)
at com.familiestvw.whatson.AllVenuesActivity.onCreate(AllVenuesActivity.java:53)
at android.app.Activity.performCreate(Activity.java:5372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$700(ActivityThread.java:159)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5419)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
at dalvik.system.NativeStart.main(Native Method)
Here is my Java code, sorry its the whole activity but i literally have no idea where the error is occuring or even what it is, im kinda new to android: 这是我的Java代码,对不起整个活动,但是我真的不知道错误发生在哪里甚至是什么,对android来说有点新:
package com.familiestvw.whatson;
public class AllVenuesActivity extends ListActivity {
// Progress Dialog
private ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> venuesList;
// url to get all venues list
private static String url_all_venues = "http://10.0.2.2/android_connect/get_all_venues.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_VENUES = "venues";
private static final String TAG_VENUE_ID = "Venue_ID";
private static final String TAG_VENUE_NAME = "Venue_Name";
// venues JSONArray
JSONArray venues = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_venues);
// Hashmap for ListView
venuesList = new ArrayList<HashMap<String, String>>();
// Loading venues in Background Thread
new LoadAllVenues().execute();
// Get listview
ListView lv = getListView();
}
/**
* Background Async Task to Load all venues by making HTTP Request
* */
class LoadAllVenues extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AllVenuesActivity.this);
pDialog.setMessage("Loading venues. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All venues from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_venues, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Venues: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// venues found
// Getting Array of Venues
venues = json.getJSONArray(TAG_VENUES);
// looping through All Venues
for (int i = 0; i < venues.length(); i++) {
JSONObject c = venues.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_VENUE_ID);
String name = c.getString(TAG_VENUE_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_VENUE_ID, id);
map.put(TAG_VENUE_NAME, name);
// adding HashList to ArrayList
venuesList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllVenuesActivity.this, venuesList,
R.layout.list_item, new String[] { TAG_VENUE_ID,
TAG_VENUE_NAME},
new int[] { R.id.Venue_ID, R.id.Venue_Name });
// updating listview
setListAdapter(adapter);
}
});
}
}
}
Any ideas?? 有任何想法吗??
It usually happens when you are trying try to dismiss any dialog which is no longer created or exist. 当您尝试关闭不再创建或不存在的对话框时,通常会发生这种情况。
Possible reasons are 可能的原因是
Your activity is no longer exists but your task is still running and trying to dismiss the dialog. 您的活动不再存在,但您的任务仍在运行,并试图关闭该对话框。
Your app is crashing somewhere in doInBackgrond. 您的应用程序在doInBackgrond中崩溃。
Check values of yopur JsonObject "c" and better to add null checks for JsonObject or wherever there is a chance for NullPointerException occurrence in your code (doInbackground here). 检查yopur JsonObject“ c”的值,最好为JsonObject或在代码中可能出现NullPointerException的地方添加空检查(此处为doInbackground)。
onPostExcute runs in Main/UI thread not in background thread. onPostExcute在Main / UI线程中运行,而不在后台线程中运行。
EDIT : 编辑:
Replace your doInBackground
code with this and make sure you are not switching the Activity in between the task is running : doInBackground
替换您的doInBackground
代码,并确保您没有在任务运行之间切换Activity:
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_venues, "GET", params);
if(json!=null){
// Check your log cat for JSON reponse
Log.d("All Venues: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// venues found
// Getting Array of Venues
venues = json.getJSONArray(TAG_VENUES);
// looping through All Venues
if(venues !=null){
for (int i = 0; i < venues.length(); i++) {
JSONObject c = venues.getJSONObject(i);
// Storing each json item in variable
if(c!=null){
String id = c.getString(TAG_VENUE_ID);
String name = c.getString(TAG_VENUE_NAME);
}
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_VENUE_ID, id);
map.put(TAG_VENUE_NAME, name);
// adding HashList to ArrayList
venuesList.add(map);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
I recommended you to use one General class for ex : Utils.java By using this class you can use progressDialog in any activity. 我建议您为ex使用一个通用类:Utils.java通过使用此类,您可以在任何活动中使用progressDialog。
Utils.java 实用工具
private static ProgressDialog progressDialog = null;
public static void showProgressDialog(Context context, String title, String message)
{
progressDialog = new ProgressDialog(context);
progressDialog.setTitle(title);
progressDialog.setMessage(message);
progressDialog.setCancelable(false);
progressDialog.show();
}
public static void dismissProgressDialog()
{
progressDialog.dismiss();
}
public static void clearDialog()
{
progressDialog = null;
}
And to show progressDialog call : 并显示progressDialog调用:
class LoadAllVenues extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
Utils.clearDialog();
Utils.showProgressDialog(Youactivity.this, "Title", "Please wait...");
}
/**
* getting All venues from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_venues, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Venues: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// venues found
// Getting Array of Venues
venues = json.getJSONArray(TAG_VENUES);
// looping through All Venues
for (int i = 0; i < venues.length(); i++) {
JSONObject c = venues.getJSONObject(i);
// Storing each json item in variable
String id = c.getString(TAG_VENUE_ID);
String name = c.getString(TAG_VENUE_NAME);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
map.put(TAG_VENUE_ID, id);
map.put(TAG_VENUE_NAME, name);
// adding HashList to ArrayList
venuesList.add(map);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
Utils.dismissProgressDialog();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllVenuesActivity.this, venuesList,
R.layout.list_item, new String[] { TAG_VENUE_ID,
TAG_VENUE_NAME},
new int[] { R.id.Venue_ID, R.id.Venue_Name });
// updating listview
setListAdapter(adapter);
}
});
}
}
This error will happened if your activity has been destroyed but you dialog is still showing. 如果您的活动已被破坏但对话框仍在显示,则会发生此错误。 So You have added these code in your activity's onDestroy() 所以您已经在活动的onDestroy()中添加了这些代码
@Override
public void OnDestory() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
} }
it may help you. 它可能会帮助您。
Use this below code in your onPostExecute
method. 在onPostExecute
方法中使用以下代码。
if (pDialog!= null) {
pDialog.dismiss();
pDialog= null;
}
So your method will look something like this. 因此,您的方法将如下所示。
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
Utils.dismissProgressDialog();
if (pDialog!= null) {
pDialog.dismiss();
pDialog= null;
}
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
* */
ListAdapter adapter = new SimpleAdapter(
AllVenuesActivity.this, venuesList,
R.layout.list_item, new String[] { TAG_VENUE_ID,
TAG_VENUE_NAME},
new int[] { R.id.Venue_ID, R.id.Venue_Name });
// updating listview
setListAdapter(adapter);
}
});
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.