Activity 的 OnStart() 中的 SKMaps Android 跟踪错误

SKMaps Android trace error in Activity's OnStart()

我最近升级了我的应用程序以使用 SKMaps 3.0.2,一切似乎都正常工作,除了当我的 activity ("ViewLocationActivity") 调用扩展 android.support.v4.app.Fragment ("NewMapFragment") 在后台运行一段时间后重新启动,并调用 onStart() 方法,Fragment 无法膨胀 SKMapViewHolder 并崩溃。我附上了日志,但看起来这是因为

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.skobbler.ngx.map.SKMapViewStyle.getStyleFileName()' on a null object reference at com.skobbler.ngx.map.SKMapViewHolder.a(SourceFile:254)

这在以前的 SDK 版本中没有发生,并且特别奇怪,因为它不会在第一次加载 Fragment 时发生,而只会在随后的重启中调用 onStart() 时发生。

相关代码片段

@Override
public void onStart() // In ViewLocationActivity {
    super.onStart();
    Intent intent = new Intent(this, LocationService.class);
    bindService(intent, mConnection, BIND_AUTO_CREATE);
}



public void onCreate(Bundle savedInstanceState) // in NewMapFragment {
    super.onCreate(savedInstanceState);
    Methods.initializeSKMaps(getActivity());        
    locationManager = (LocationManager)getActivity().getSystemService(Context.LOCATION_SERVICE);
    registerLocationListener();
    SKCurrentPositionProvider currentPositionProvider = new SKCurrentPositionProvider(getActivity());
    currentPositionProvider.setCurrentPositionListener(this);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) // in NewMapFragment {
    // App crashes in this line
    mView = inflater.inflate(R.layout.fragment_new_map, container, false);
    mapHolder = (SKMapViewHolder)mView.findViewById(R.id.map_surface_holder);
    mapHolder.setMapSurfaceListener(this);
    initializeControlButtons();
    initializeBottomSheet();
    return mView;
}

    <!-- fragment_new_map -->
<android.support.design.widget.CoordinatorLayout
    android:id="@+id/skobbler_map_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <com.skobbler.ngx.map.SKMapViewHolder
        android:id="@+id/map_surface_holder"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />
    <include layout="@layout/map_control_layout"
        android:id="@+id/map_control_layout"/>
</android.support.design.widget.CoordinatorLayout>

Logcat:

01-23 20:40:51.548 19453-19453/com.shay.vagabond E/AndroidRuntime: FATAL EXCEPTION: main Process: com.shay.vagabond, PID: 19453 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.shay.vagabond/com.shay.vagabond.Activities.ViewLocationActivity}: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.skobbler.ngx.map.SKMapViewHolder at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 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:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: android.view.InflateException: Binary XML file line #7: Binary XML file line #7: Error inflating class com.skobbler.ngx.map.SKMapViewHolder Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class com.skobbler.ngx.map.SKMapViewHolder Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:430) at android.view.LayoutInflater.createView(LayoutInflater.java:645) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) at android.view.LayoutInflater.inflate(LayoutInflater.java:518) at android.view.LayoutInflater.inflate(LayoutInflater.java:426) at com.shay.vagabond.Fragments.NewMapFragment.onCreateView(NewMapFragment.java:155) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1277) at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2154) at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603) at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181) at com.shay.vagabond.Activities.ViewLocationActivity.onStart(ViewLocationActivity.java:691) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248) at android.app.Activity.performStart(Activity.java:6681) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609) 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:865) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.skobbler.ngx.map.SKMapViewStyle.getStyleFileName()' on a null object reference at com.skobbler.ngx.map.SKMapViewHolder.a(SourceFile:254) at com.skobbler.ngx.map.SKMapViewHolder.(SourceFile:159) at java.lang.reflect.Constructor.newInstance0(Native Method)  at java.lang.reflect.Constructor.newInstance(Constructor.java:430)  at android.view.LayoutInflater.createView(LayoutInflater.java:645)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)  at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)  at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)  at android.view.LayoutInflater.inflate(LayoutInflater.java:518)  at android.view.LayoutInflater.inflate(LayoutInflater.java:426)  at com.shay.vagabond.Fragments.NewMapFragment.onCreateView(NewMapFragment.java:155)  at android.support.v4.app.Fragment.performCreateView(Fragment.java:2087)  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1113)  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1295)  at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1277)  at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:2154)  at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:201)  at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:603)  at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:181)  at com.shay.vagabond.Activities.ViewLocationActivity.onStart(ViewLocationActivity.java:691)  at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1248)  at android.app.Activity.performStart(Activity.java:6681)  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2609)  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:865)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

我不知道这是否是一个理想的解决方案,但由于我没有得到关于 Skobbler SDK 中可能导致此行为的原因的答案,我设法通过简单地删除有问题的片段来解决这个问题,它在调用 onCreate() 时从后台堆栈膨胀并包含 SKMapsViewHolder(如果进程被 OS 销毁则调用)。这迫使 Activity 到 re-initialize 并重新创建片段,这似乎解决了问题。希望它在某些时候对某人有所帮助

    private void initializeViews() {
    // Setting the pre-arrival layout
    setContentView(R.layout.view_location_layout);
    initializeNavigationDrawer();
    initializeBottomSheet();
    initializeProgressBar();
    removeAllFragments();
}

private void removeAllFragments() {
    while (fragmentManager.getBackStackEntryCount() > 0)
        fragmentManager.popBackStackImmediate();
}

此问题已修复,将在下一个 SKMaps 发布版本中提供。