【问题标题】:Android: strange npe on MapView onDestroy inside an activityAndroid:活动内的 MapView onDestroy 上的奇怪 npe
【发布时间】:2016-08-09 01:41:12
【问题描述】:

我在 Activity 中使用 mapview,但收到一个奇怪的空指针异常,我似乎无法查明原因。

xml:

<com.google.android.gms.maps.MapView
                    xmlns:map="http://schemas.android.com/apk/res-auto"
                    android:id="@+id/myMapView"
                    android:layout_width="match_parent"
                    android:layout_height="156dp"
                    map:cameraZoom="16"
                    map:liteMode="true"
                    android:visibility="invisible"
                    />

然后在activity里面:

private MapView mMapView;

在 onCreate 中初始化:

mMapView = (MapView) findViewById(R.id.myMapView);

并覆盖方法:

mMapView.onCreate(savedInstanceState);

@Override
protected void onDestroy() {
    super.onDestroy();
    mMapView.onDestroy();

}


@Override
public void onLowMemory() {
    super.onLowMemory();
    mMapView.onLowMemory();

}


@Override
protected void onPause() {
    super.onPause();
    mMapView.onPause();

}


@Override
protected void onResume() {
    super.onResume();
    mMapView.onResume();


}

我不断收到 mapview onDestroy 方法的崩溃,其堆栈跟踪如下:

java.lang.RuntimeException: Unable to destroy activity {myActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.isEmpty()' on a null object reference
                                                                           at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3831)
                                                                           at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849)
                                                                           at android.app.ActivityThread.-wrap5(ActivityThread.java)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                           at android.os.Looper.loop(Looper.java:148)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                        Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.LinkedList.isEmpty()' on a null object reference
                                                                           at com.google.android.gms.dynamic.zza.zzeJ(Unknown Source)
                                                                           at com.google.android.gms.dynamic.zza.onDestroy(Unknown Source)
                                                                           at com.google.android.gms.maps.MapView.onDestroy(Unknown Source)
                                                                           at myActivity.onDestroy(myActivity.java:454)
                                                                           at android.app.Activity.performDestroy(Activity.java:6422)
                                                                           at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1142)
                                                                           at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3818)
                                                                           at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3849) 
                                                                           at android.app.ActivityThread.-wrap5(ActivityThread.java) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1398) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                           at android.os.Looper.loop(Looper.java:148) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

鉴于 mmapview 在 onCreate 开始时从 xml 初始化并且在 onDestroy 之前一直存在,这个空指针的原因可能是什么?

【问题讨论】:

  • 如果您删除super.onDestroy(); 或将此行移到下方会怎样?
  • @ShreeKrishna 有趣的想法 - 我会试试的。谢谢!
  • 不幸的是,崩溃仍然发生
  • 您是否尝试过检查是否不为空,如答案?
  • @ShreeKrishna 是的,我有 - 这是我第一次开始遇到错误时尝试的第一件事

标签: android google-maps nullpointerexception google-maps-android-api-2 android-mapview


【解决方案1】:

最终结果是,原因是我将 mapview.oncreate 放在了一个未执行的 if 语句中。一旦我将它移到 oncreate 方法的最开始,错误就停止发生了。

【讨论】:

    【解决方案2】:

    如果你确定在onDestroy() 之前没问题,我建议这样做,

    @Override
    protected void onDestroy() {
        if(mMapView != null) {
            mMapView.onDestroy();
        }
        super.onDestroy();
    }
    

    【讨论】:

      【解决方案3】:

      我认为您不必担心在视图上调用 onDestroy()。 我可能是错的,但是当活动被破坏为 XML 上的视图时,无论如何都应该这样做。

      如果您有其他在 onCreate() 上初始化的对象(适配器或演示者),清理它们可能是个好主意(调用释放方法/将它们设置为 null 或任何需要的)

      除了我遵循的一个很好的规则是 onPause/onDestroy Shree Krishna 建议:清理你的资源,然后调用 super。

      @Override
          protected void onPause() {
              // clean up your resources in respect to your onResume
              ...
              super.onPause();
          }
      
      @Override
      protected void onDestroy() {
          // clean up your resources in respect of onCreate
          ...
          super.onDestroy();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-10-10
        • 2021-04-23
        • 2020-12-09
        • 2013-02-28
        • 2020-03-09
        相关资源
        最近更新 更多