简体   繁体   English

如何修复我的 savedInstanceState 上的 NullPointerException 以及它发生的原因

[英]how to fix NullPointerException on my savedInstanceState and why its happend

im has build this mapping application, before on separated android project and its work.我已经构建了这个映射应用程序,在分离的 android 项目和它的工作之前。 when i try to integrate with another system, the problem is occur, i have copy all code same as previous project is mapping system.当我尝试与另一个系统集成时,问题出现了,我复制了与以前的项目映射系统相同的所有代码。 the problem is there null on my "savedInstanceState" and how i cant fix it?问题是我的“savedInstanceState”上有空值,我怎么解决?

this for my mapping system for new project, running on mobile phone android 9. i want integrate with project A("..") and project B("mapping system")before integrate all is totally work.这是我的新项目的映射系统,在手机 android 9 上运行。我想在集成之前与项目 A(“..”)和项目 B(“映射系统”)集成完全工作。


-error message- on  mapView.onCreate(savedInstanceState);

private MapView mapView;
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 
'void com.mapbox.mapboxsdk.maps.MapView.onCreate(android.os.Bundle)' 
on a null object reference


  @Override
  protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

            Mapbox.getInstance(this,   "......");
            setContentView(R.layout.activity_main);
            mapView = findViewById(R.id.mapView);

            mapView.onCreate(savedInstanceState);
            mapView.getMapAsync(this);
    }

   @Override
    public void onSaveInstanceState(Bundle outState, PersistableBundle   outPersistentState) {
        super.onSaveInstanceState(outState, outPersistentState);
        mapView.onSaveInstanceState(outState);
    }

expected result: Map is show预期结果:显示地图

actual result: the app is crash because, there null value on my "mapView.onCreate(savedInstanceState);"实际结果:应用程序崩溃,因为我的“mapView.onCreate(savedInstanceState);”上有空值

savedInstanceState is null when the activity first starts.当活动第一次启动时, savedInstanceState为空。

The safest way to call it is to make sure it's not null before using it anywhere.调用它的最安全方法是在任何地方使用它之前确保它不为空。

if(savedInstanceState != null) {
   mapView.onCreate(savedInstanceState);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM