2013-08-28 42 views
3

我正在使用使用Google地图的Android应用程序。该应用程序最常用。我可以在logcat中看到一些错误[下方]。Android泄漏窗口 - 应用程序采用ANR状态

logcat的

08-28 15:37:36.945: E/WindowManager(21969): Activity com.kod.example.Find_Tab_MainActivity has leaked window [email protected] that was originally added here 
08-28 15:37:36.945: E/WindowManager(21969): android.view.WindowLeaked: Activity com.kod.example.Find_Tab_MainActivity has leaked window [email protected] that was originally added here 
08-28 15:37:36.945: E/WindowManager(21969):  at android.view.ViewRoot.<init>(ViewRoot.java:263) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:171) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:114) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.view.Window$LocalWindowManager.addView(Window.java:424) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.Dialog.show(Dialog.java:241) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.kod.example.MyProgressDialog.show(MyProgressDialog.java:36) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.kod.example.MyProgressDialog.show(MyProgressDialog.java:19) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.kod.example.MyProgressDialog.show(MyProgressDialog.java:14) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.kod.example.Find_Tab_MainActivity$LoadPlaces.onPreExecute(Find_Tab_MainActivity.java:129) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.os.AsyncTask.execute(AsyncTask.java:391) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.kod.example.Find_Tab_MainActivity.onCreate(Find_Tab_MainActivity.java:112) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.os.Handler.dispatchMessage(Handler.java:99) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.os.Looper.loop(Looper.java:130) 
08-28 15:37:36.945: E/WindowManager(21969):  at android.app.ActivityThread.main(ActivityThread.java:3687) 
08-28 15:37:36.945: E/WindowManager(21969):  at java.lang.reflect.Method.invokeNative(Native Method) 
08-28 15:37:36.945: E/WindowManager(21969):  at java.lang.reflect.Method.invoke(Method.java:507) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 
08-28 15:37:36.945: E/WindowManager(21969):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 
08-28 15:37:36.945: E/WindowManager(21969):  at dalvik.system.NativeStart.main(Native Method) 

这是什么泄露的窗口?我的应用程序工作正常,但有时会崩溃,始终引发此错误。

我的代码

package com.kod.example; 

import java.util.List; 

import android.app.AlertDialog; 
import android.app.ProgressDialog; 
import android.content.Context; 
import android.content.DialogInterface; 
import android.content.Intent; 
import android.location.Address; 
import android.location.Geocoder; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.widget.Button; 

import com.google.android.maps.MapActivity; 


public class Find_Tab_MainActivity extends MapActivity { 

    // flag for Internet connection status 
    Boolean isInternetPresent = false; 

    Geocoder geocoder; 

    String status; 

    // Connection detector class 
    ConnectionDetector cd; 

    // Alert Dialog Manager 
    AlertDialogManager alert = new AlertDialogManager(); 

    // Google Places 
    GooglePlaces googlePlaces; 

    // Places List 
    PlacesList nearPlaces; 

    Place reference; 
    // GPS Location 
    GPSTracker gps; 
    double latitude; 
    double longitude; 
    // Button 
    Button loginbtn; 

    Place p; 

    Intent i; 

    // Progress dialog 
    ProgressDialog pDialog; 
    final Context context = this; 

    String type,KEY_KEYWORD,KEY_TAG; 



    // KEY Strings 
    public static String KEY_REFERENCE = "reference"; // id of the place 
    public static String KEY_NAME = "name"; // name of the place 
    public static String KEY_VICINITY = "vicinity"; 
    public static String KEY_NUMBER = "formatted_phone_number"; 


    // Place area name 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

     cd = new ConnectionDetector(getApplicationContext()); 


     Intent entent = getIntent(); 

      type = entent.getExtras().getString("KEY_TYPES"); 
      KEY_KEYWORD = entent.getExtras().getString("KEY_KEYWORD"); 
      KEY_TAG = entent.getExtras().getString("KEY_TAG"); 


     // Check if Internet present 
     isInternetPresent = cd.isConnectingToInternet(); 
     if (!isInternetPresent) { 
      // Internet Connection is not present 
      alert.showAlertDialog(Find_Tab_MainActivity.this, "Internet Connection Error", 
        "Please connect to working Internet connection", false); 
      // stop executing code by return 
      return; 
     } 

     // creating GPS Class object 
     gps = new GPSTracker(this); 

     // check if GPS location can get 
     if (gps.canGetLocation()) { 

     } else { 
      // Can't get user's current location 
      alert.showAlertDialog(Find_Tab_MainActivity.this, "GPS Status", 
        "Couldn't get location information. Please enable GPS", 
        false); 
      // stop executing code by return 
      return; 
     } 


     // calling background Async task to load Google Places 
     // After getting places from Google all the data is shown in listview 
     new LoadPlaces().execute(); 

     /** Button click event for shown on map */ 

      } 

    /** 
    * Background Async Task to Load Google places 
    * */ 
    class LoadPlaces extends AsyncTask<String, String, String> { 

     /** 
     * Before starting background thread Show Progress Dialog 
     * */ 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      MyProgressDialog.show(Find_Tab_MainActivity.this, "", ""); 
      } 

     /** 
     * getting Places JSON 
     * */ 
     protected String doInBackground(String... args) { 
      // creating Places class object 
      googlePlaces = new GooglePlaces(); 

      try { 
       // Separeate your place types by PIPE symbol "|" 
       // If you want all types places make it as null 
       // Check list of types supported by google 
       String types = type; // Listing places only for park 

       // Radius in meters - increase this value if you don't find any places 
       double radius = 10000; // 1000 meters 

       if(KEY_TAG.equalsIgnoreCase("1")||KEY_TAG.equalsIgnoreCase("4")||KEY_TAG.equalsIgnoreCase("5")) 
       { 
        // get nearest places 
        nearPlaces = googlePlaces.search(gps.getLatitude(),gps.getLongitude(), radius, types); 
       } 
       else if(KEY_TAG.equalsIgnoreCase("2")||KEY_TAG.equalsIgnoreCase("3")) 
       { 
        // get nearest places 
        nearPlaces = googlePlaces.StreetGaragesearch(gps.getLatitude(), gps.getLongitude(), radius, types,KEY_KEYWORD); 
       } 

       else if(KEY_TAG.equalsIgnoreCase("6")) 
       { 
        String addressInput = type; 

        geocoder = new Geocoder(Find_Tab_MainActivity.this); 
       List<Address> address; 

        address = geocoder.getFromLocationName(addressInput,1); 
         if (address == null) { 
          alert.showAlertDialog(Find_Tab_MainActivity.this, "Places Error", 
             "Sorry error occured.", 
             false); 
         } 
         else{ 
         Address location = address.get(0); 
         latitude = location.getLatitude(); 
         longitude = location.getLongitude(); 

        nearPlaces = googlePlaces.Keywordsearch(latitude,longitude,radius+40000,"parking",type); 
         } 

        // get nearest places 


      } 
      } 
      catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     /** 
     * After completing background task Dismiss the progress dialoghis 
     * and show the data in UI 
     * Always use runOnUiThread(new Runnable()) to update UI from background 
     * thread, otherwise you will get error 
     * **/ 
     @SuppressWarnings("deprecation") 
     protected void onPostExecute(String file_url) { 
      try{ 
     // Get json response status 
      status = nearPlaces.status; 

      // updating UI from Background Thread 
      runOnUiThread(new Runnable() { 
       public void run() { 


        if(status.equals("OK")){ 
         // Successfully got places details 
         i = new Intent(getApplicationContext(), 
           MyPlacesMapActivity.class); 
         // Sending user current geo location 

         // passing near places to map activity 
         i.putExtra("near_places", nearPlaces); 

         //passing type to placemapactivity to identify the overlay icon 
         i.putExtra("KEY_TAG", KEY_TAG); 

         //passing place reference 
         i.putExtra("place_reference", reference); 

         // staring activity 
         startActivity(i); 
           } 

        else if(status.equals("ZERO_RESULTS")){ 
         // Zero results found 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("No Results"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry no places found. Try to change the location"); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 
        } 
        else if(status.equals("UNKNOWN_ERROR")) 
        { 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("Error"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry unknown error occured."); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 

        } 
        else if(status.equals("OVER_QUERY_LIMIT")) 
        { 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("Error"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry query limit to is reached"); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 

        } 
        else if(status.equals("REQUEST_DENIED")) 
        { 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("Error"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry error occured. Request is denied"); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 

        } 
        else if(status.equals("INVALID_REQUEST")) 
        { 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("Error"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry error occured. Invalid Request"); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 
        } 
        else 
        { 

         final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

         // Setting Dialog Title 
         alertDialog.setTitle("Error"); 

         // Setting Dialog Message 
         alertDialog.setMessage("Sorry error occured."); 

         // Setting alert dialog icon 
          alertDialog.setIcon(R.drawable.fail); 

         // Setting OK Button 
         alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
          public void onClick(DialogInterface dialog, int which) { 
           Find_Tab_MainActivity.this.finish(); 
          } 
         }); 

         // Showing Alert Message 
         alertDialog.show(); 
        } 
       } 
      }); 
     }catch(NullPointerException e){ 

      final AlertDialog alertDialog = new AlertDialog.Builder(context).create(); 

      // Setting Dialog Title 
      alertDialog.setTitle("Error"); 

      // Setting Dialog Message 
      alertDialog.setMessage("Requested place not found."); 

      // Setting alert dialog icon 
       alertDialog.setIcon(R.drawable.fail); 

      // Setting OK Button 
      alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int which) { 
        Find_Tab_MainActivity.this.finish(); 
       } 
      }); 

      // Showing Alert Message 
      alertDialog.show(); 

     } 

     } 
    } 

    @Override 
    protected boolean isRouteDisplayed() { 
     // TODO Auto-generated method stub 
     return false; 
    } 

} 

回答

6
What is this leaked window? 

你的情况WindowLeaked exception正在发生的事情,因为你没有完成活动之前驳回MyProgressDialog。因此它泄漏了与之相关的内存。

一般情况下:

WindowLeaked exception当你有某种形式的异步任务是在这个开始它的活动结束后,一般情况,在案件发生destroyed.This型异常的主要表现为Dialogs,在活动上下文被销毁之前,我们没有关闭对话框。

所以这是为了更好地显示该对话框之前把一张支票,如下:

if(getActivity()!= null && !getActivity().isFinishing()){ 
      Dialog.show(); 
} 

解决方案针对上述问题是dismissMyProgressDialog之前完成的活动。

修改代码如下:

protected void onPostExecute(String file_url) { 

    // your first line should be this 

    if (MyProgressDialog!=null && MyProgressDialog.isShowing()){ 
     MyProgressDialog.dismiss(); 
    } 

    // rest of the code 
} 
2

有问题与警告对话框地方。您不是在任何地方解除您启动的对话框,也不会拨打.dismiss()。此外,仅供参考,您不需要onPostExecute中的runOnUiThread,因为此函数在UI线程本身中运行。

1

在退出您的活动之前您创建的对话框上呼叫解除(),例如,在的onPause()