活动已泄露窗口DecorView @ 92f9f38 [],此处最初添加错误

时间:2018-04-26 10:32:31

标签: android google-maps

我在Google地图中编写了一个代码,以便hospitalsrestaurantsschools加载。我正在使用这些依赖项:implementation 'com.google.android.gms:play-services-maps:15.0.0'implementation 'com.google.android.gms:play-services-location:15.0.0'。我正在使用Gradle 3.0.0。当我运行我的代码时,我收到此错误:

Activity has leaked window DecorView@92f9f38[] that was originally added here
   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:417)
   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:331)
   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:93)
   at android.app.Dialog.show(Dialog.java:316)
   at com.dell.spacestemp.ui.Activities.NearByActivity.CheckGooglePlayServices(NearByActivity.java:81)
   at com.dell.spacestemp.ui.Activities.NearByActivity.onCreate(NearByActivity.java:61)
   at android.app.Activity.performCreate(Activity.java:6662)
   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
   at android.app.ActivityThread.-wrap12(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
   at android.os.Handler.dispatchMessage(Handler.java:102)
   at android.os.Looper.loop(Looper.java:154)
   at android.app.ActivityThread.main(ActivityThread.java:6077)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)  

此错误发生在此代码块中:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            checkLocationPermission();
        }

        //Check if Google Play Services Available or not
        if (!CheckGooglePlayServices()) {
            Log.d("onCreate", "Finishing test case since Google Play Services are not available");
            finish();
        }
        else {
            Log.d("onCreate","Google Play Services available.");
        }

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }



private boolean CheckGooglePlayServices() {
        GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance();
        int result = googleAPI.isGooglePlayServicesAvailable(this);
        if(result != ConnectionResult.SUCCESS) {
            if(googleAPI.isUserResolvableError(result)) {
                googleAPI.getErrorDialog(this, result,
                        0).show();
            }
            return false;
        }
        return true;
    }  

我该如何解决?是什么造成的?为什么会这样?

2 个答案:

答案 0 :(得分:1)

将片段事务代码放在

的else部分
if (!CheckGooglePlayServices()) {
 ......
}
else {
    Log.d("onCreate","Google Play Services available.");
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
}

或者你可以试试;

if(!((Activity)YourActivity.this).isFinishing()) {
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
     mapFragment.getMapAsync(this);
}

下面提到的代码使用分离的线程来执行..

  int result = googleAPI.isGooglePlayServicesAvailable(this);
  

这种情况正在发生,因为您的活动正在片段交易期间完成!!

答案 1 :(得分:0)

我认为您的代码在销毁活动后尝试获取上下文