2014-02-10 49 views
1

IM书面方式一个应用程序,从数据库中获取数据。然而即时得到这个错误:

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

    } 

} 
} 

任何想法?

+1

你可以发布你的完整logcat? – InnocentKiller

+0

当'AsyncTask'正在做它的工作时,你是否从'AllVenuesActivity'导航? –

+0

检查你的logcat给它上面的另一个。 –

回答

4

它通常发生在您尝试尝试解除任何不再创建或存在的对话框时。

可能的原因是

  • 你的活动不再存在,但你的任务仍在运行,并试图关闭对话框。

  • 您的应用在doInBackgrond某处崩溃。

yopur JSONObject的“c”和更好的校验值加空检查JSONObject的或只要有对空指针异常发生在你的代码(doInbackground这里)的机会。

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; 
} 
+0

,所以我需要添加什么代码? – user3001471

+0

谢谢你,但在行map.put(TAG_VENUE_ID,id);和一个下面即时通讯得到一个错误消息说不能解析符号ID和名称 – user3001471

+0

我没有改变你的代码符号是由你定义的,我只是插入了空的检查,你可以检查我的代码并把它放在你自己的代码中,我已经将你的变量复制到了我的代码中,没有错误。错过任何大括号 – pyus13

0

如果您的活动已被破坏但您的对话仍在显示,则会发生此错误。所以,你必须在你的活动的的onDestroy()

@Override 
public void OnDestory() { 
if (dialog != null) { 
    dialog.dismiss(); 
    dialog = null; 
} 

}

它可以帮助您添加了这些代码。

+0

我应该在哪里把这个放在我的课堂内? – user3001471

+0

只需在onpostexecute方法后调用它。 – rajshree

+0

仍然得到相同的错误:( – user3001471

1

我推荐你使用为前一类通用: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; 
} 

,并显示出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); 
      } 
     }); 

    } 

} 
+0

,但我不建议使用静态引用的对象绑定到'上下文',这里是'ProgressDialog' ... –

+0

使用后,我们可以通过调用Utils.clearDialogs()来释放它的内存。 –

+0

这个,但同样的错误? – user3001471

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

    } 
+0

这是一个Mehul Ranpara发布的扩展或应该我的代码添加到我的原始? – user3001471

+0

在你原来的一本中,我只编辑了一本,不需要使用任何其他代码。 – InnocentKiller

+0

在我原来的代码行Utils.dismissProgressDialog();不存在我应该添加吗? – user3001471

相关问题