“窗口管理BadTokenException,无法添加窗口”为什么我收到此异常,而使用谷歌地图?(“

2019-10-22 13:04发布

我使用谷歌地图显示(销)不同places.After显示不同palces打它重定向到我家screen.Around 10-15秒后,我的应用程序被closed.I认为,位置更新中仍在运行的后退按钮时这将导致此error.How停止在后台更新活动的背景是什么?

    package com.bar.start;

    import java.util.ArrayList;
    import org.apache.http.HttpEntity;
    import org.apache.http.conn.ClientConnectionManager;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager;
    import org.apache.http.params.HttpParams;
    import com.bar.barapp.R;
    import com.bar.location.Loc;
    import com.bar.location.Webcall;
    import com.google.android.gms.maps.CameraUpdateFactory;
    import com.google.android.gms.maps.GoogleMap;
    import com.google.android.gms.maps.MapFragment;
    import com.google.android.gms.maps.model.BitmapDescriptorFactory;
    import com.google.android.gms.maps.model.Circle;
    import com.google.android.gms.maps.model.LatLng;
    import com.google.android.gms.maps.model.MarkerOptions;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.content.Context;
    import android.content.SharedPreferences;
    import android.location.Location;
    import android.location.LocationListener;
    import android.location.LocationManager;
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.print.PrintAttributes;
    import android.util.Log;
    import android.widget.ArrayAdapter;

    public class LocationActivity extends Activity implements LocationListener {

     private final String TAG = getClass().getSimpleName();
     protected ConnectionDetector connectionDetector;
     public Boolean isInternetPresent;
     private String[] places;
     public static Loc [] getloc;
     private LocationManager locationManager;
     private Location loc;

     static HttpEntity entityResponse = null;
     GoogleMap map;
     MarkerOptions mp;
     Activity activity;
     Context context;
     LocationListener listener;
     Double lat,lon;
    final   String Googlekey="APikey" ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {



        SharedPreferences sharedPreferences;
        int locationCount = 0;
        connectionDetector = new ConnectionDetector(getApplicationContext());
        isInternetPresent = connectionDetector.isConnectingToInternet();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.location);

     LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

      lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 1000, 0, this);

      map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();

    }

    @Override
    public void onLocationChanged(Location location) {

        map.clear();
        lat=location.getLatitude();
        lon=location.getLongitude();

        Googleapicall exeTask = new Googleapicall();
          exeTask.execute();

          mp = new MarkerOptions();

           mp.position(new LatLng(lat, lon));
           map.addMarker(mp);
           mp.title("My Location");
   p.animateCamera(CameraUpdateFactory.newLatLngZoom(
          new LatLng(lat, lon), 13));

    }

    @Override
    public void onProviderDisabled(String provider) {


    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }



    public class Googleapicall extends AsyncTask<String, Void, String>

    {


        Loc statStr = null;
         Loc[] statInfoResult = null;
         ArrayList<Loc> SIArrayList = new ArrayList<Loc>();

        private ProgressDialog Dialog;

        @Override
        protected void onPreExecute() {
            Dialog = new ProgressDialog(LocationActivity.this);
            Dialog.setMessage(LocationActivity.this.getResources().getString(
                    R.string.loading));
            Dialog.setCancelable(false);
            Dialog.show();

        }

        @Override
        protected String doInBackground(String... params) {

            String result = "";
            try 
            {

                if(isInternetPresent)

                {
                getloc=Webcall.getdet(lat,lon,Googlekey);
                result = "Success";
                System.out.println("getloc "+getloc.length);

                }


                else 
                {
                    result = "Failure";
                }
            } 


            catch (Exception e)
            {
                result = "Failure";
                e.printStackTrace();
            }

            return result;


        }

        @Override
        protected void onPostExecute(String result) {


            Loc lc=new Loc();

            try {

                if(result.contains("Success"))
                {

                    mp=new MarkerOptions();

                    for(int i=0;i<getloc.length;i++)
                    {
                        Double lati=getloc[i].latitudes;
                        Double longi=getloc[i].longitudes;
                           map.addMarker(new MarkerOptions()
                              .title("Nearbyplace")
                              .position(
                                new LatLng(lati,longi))
                              .icon(BitmapDescriptorFactory.fromResource(R.drawable.small)));
                    }
                }

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

            }

            Dialog.dismiss();

        }

    }


}


04-06 11:09:53.768: E/AndroidRuntime(5188):     FATAL EXCEPTION: main
04-06 11:09:53.768: E/AndroidRuntime(5188):     android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@42ea1380 is not valid; is your activity running?
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:796)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:288)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.Dialog.show(Dialog.java:287)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity$Googleapicall.onPreExecute(LocationActivity.java:194)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.AsyncTask.execute(AsyncTask.java:534)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.bar.start.LocationActivity.onLocationChanged(LocationActivity.java:104)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:255)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:184)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:200)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.os.Looper.loop(Looper.java:137)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at java.lang.reflect.Method.invoke(Method.java:525)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025)
04-06 11:09:53.768: E/AndroidRuntime(5188):     at dalvik.system.NativeStart.main(Native Method)

Answer 1:

在全球范围内宣布该值。

int i=0;

在上面的代码进行以下changes.So,它会更新一次。(窗口管理器badtokenexception,无法添加窗口)这个异常不会发生again.But我不知道它是否是do.If人有更好的答案正确的方法,别客气。

@Override
        public void onLocationChanged(Location location) {


               for(;i<=0;i++)
               {

                   map.clear();

                    lat=location.getLatitude();
                    lon=location.getLongitude();

                      mp = new MarkerOptions();

                       mp.position(new LatLng(lat, lon));
                       map.addMarker(mp);
                       mp.title("My Location");

                   map.animateCamera(CameraUpdateFactory.newLatLngZoom(
                              new LatLng(lat, lon), 13));

                            Googleapicall exeTask = new Googleapicall();
                              exeTask.execute();

               }


        }


Answer 2:

你可以试试 ,

  1. 创建布尔静态变量,

     private static boolean isActivityOpen = false ; 
  2. 更新在的onCreate()的值

     isActivityOpen = true; 
  3. 检查值在对话框显示/辞退方法

     if(isActivityOpen ) { Dialog.show(); } 
  4. 在禁用的onDestroy值()

     isActivityOpen = false; 


文章来源: “WindowManager BadTokenException-Unable to add window”Why i am getting this exception while using google maps?