我正在编写一个从数据库获取数据的应用程序,但是我得到了这个错误:
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)
这是我的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);
}
});
}
}
}
任何想法??
答案 0 :(得分:4)
通常会在您尝试关闭任何不再创建或存在的对话框时发生。
可能的原因是
您的活动已不存在,但您的任务仍在运行,并尝试关闭该对话框。
你的应用程序在doInBackgrond的某个地方崩溃。
检查yopur JsonObject“c”的值,最好为JsonObject添加空值检查,或者在代码中有可能出现NullPointerException(doInbackground here)。
onPostExcute在Main / UI线程中运行而不是在后台线程中运行。
编辑:
用此替换您的doInBackground
代码,并确保在任务正在运行之间没有切换活动:
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;
}
答案 1 :(得分:1)
我建议你在ex:Utils.java中使用一个General类 通过使用此类,您可以在任何活动中使用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;
}
并显示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);
}
});
}
}
答案 2 :(得分:0)
如果您的活动已被销毁但对话仍在显示,则会发生此错误。所以你已经在你的活动的onDestroy()
中添加了这些代码@Override
public void OnDestory() {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
}
它可能对你有所帮助。
答案 3 :(得分:0)
在onPostExecute
方法中使用以下代码。
if (pDialog!= null) {
pDialog.dismiss();
pDialog= null;
}
所以你的方法看起来像这样。
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);
}
});
}