简体   繁体   English

Android 关闭飞行模式时应用程序崩溃

[英]Android App crushes when airplane mode is turned off

I am building an application in which I have a Google Maps Fragment.我正在构建一个应用程序,其中有一个谷歌地图片段。 In my MainActivity onCreate() method, I display this Google Maps Fragment which works fine in all cases except with airplane mode.在我的 MainActivity onCreate()方法中,我显示了这个谷歌地图片段,它在所有情况下都可以正常工作,除了飞行模式。 If I turn airplane mode off while the application is running, my app crushes.如果我在应用程序运行时关闭飞行模式,我的应用程序就会崩溃。 I tried to include some Logs to see which methods are called and apparently, the following methods are called in order:我试图包含一些日志以查看调用了哪些方法,显然,按顺序调用了以下方法:

onPause() in MapFragment MapFragment 中的onPause()

onPause() in MainActivity MainActivity 中的onPause()

onCreate() in MainActivity MainActivity 中的onCreate()

onResume() in MainActivity MainActivity 中的onResume()

and finally the app crushes in onMapReady(GoogleMap googleMap) in the MapFragment when I call最后,当我打电话时,应用程序在 MapFragment 中的onMapReady(GoogleMap googleMap)中崩溃

MapsInitializer.initialize(getActivity());

What I do not understand is how and why these methods are called when airplane mode is turned off and how to prevent the application from crushing.我不明白的是在关闭飞行模式时如何以及为什么调用这些方法以及如何防止应用程序崩溃。

Here is the stack trace of the error这是错误的堆栈跟踪

2020-12-03 05:32:44.070 700-700/com.example.mutapamaps E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.mutapamaps, PID: 700
    java.lang.NullPointerException: Context is null
        at com.google.android.gms.common.internal.Preconditions.checkNotNull(com.google.android.gms:play-services-basement@@17.3.0:11)
        at com.google.android.gms.maps.MapsInitializer.initialize(Unknown Source)
        at com.example.mutapamaps.Fragments.MapFragment.onMapReady(MapFragment.java:218)
        at com.google.android.gms.maps.zzac.zza(Unknown Source)
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:504)
        at dt.aZ(:com.google.android.gms.dynamite_mapsdynamite@204516046@20.45.16 (040306-0):2)
        at com.google.maps.api.android.lib6.impl.bi.run(:com.google.android.gms.dynamite_mapsdynamite@204516046@20.45.16 (040306-0):1)
        at android.os.Handler.handleCallback(Handler.java:836)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:203)
        at android.app.ActivityThread.main(ActivityThread.java:6251)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1063)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:924)

Here is my MainActivity这是我的 MainActivity

public class MainActivity extends AppCompatActivity{

    public static FragmentManager fragmentManager;
    public static final String LOG_TAG = "LOG_TAG";


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.i(LOG_TAG, "Main activity created");
        super.onCreate(savedInstanceState);

        // init airplane mode receiver
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_AIRPLANE_MODE_CHANGED);
        BroadcastReceiver receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                boolean isAirplaneModeOn = intent.getBooleanExtra("state", false);
                if(isAirplaneModeOn){
                    Log.i(LOG_TAG, "Airplane mode turned on");// handle Airplane Mode on
                } else {
                    Log.i(LOG_TAG, "Airplane mode turned off");
                }
            }
        };
        this.registerReceiver(receiver, intentFilter);


        setContentView(R.layout.activity_main);
        fragmentManager = getSupportFragmentManager();
        displayFragment(new MapFragment());
    }


    @Override
    protected void onPause() {
        super.onPause();
        Log.i(LOG_TAG, "Main activity paused");
    }


    @Override
    protected void onResume() {
        super.onResume();
        Log.i(LOG_TAG, "Main activity resumed");
    }


    public void displayFragment(Fragment fragmentActivity){

        // start the transaction
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment_container, fragmentActivity).
                    addToBackStack(fragmentActivity.getClass().getSimpleName()).commit();
    }
}

Here is my MapFragment这是我的 MapFragment

public class MapsFragment extends Fragment implements
        OnMapReadyCallback {


    public MapsFragment() {
        // Required empty public constructor
        super();
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_map, container, false);
    }


    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        FrameLayout map_frame = view.findViewById(R.id.map_frame);

        // configure map
        MapView mapFragment = view.findViewById(R.id.map_view);
        mapFragment.onCreate(savedInstanceState);
        mapFragment.onResume();
        mapFragment.getMapAsync(this);
        View mapView = mapFragment.getRootView();
        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        Log.i(LOG_TAG, "MapFragment resumed");
    }


    @Override
    public void onPause() {
        super.onPause();
        Log.i(LOG_TAG, "Map fragment paused");
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Log.i(LOG_TAG, "onMapReadyCalled");
        MapsInitializer.initialize(getActivity());
    }
}

May you please help me understand what is going on and debug this piece of code.请您帮助我了解发生了什么并调试这段代码。

Thank you in advance.先感谢您。

Change the onViewCreated with this part of code.使用这部分代码更改 onViewCreated。 Hope it helps.希望能帮助到你。

@Override
 public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
 FrameLayout map_frame = view.findViewById(R.id.map_frame);

 // configure map
 MapView mapFragment = view.findViewById(R.id.map_view);
 mapFragment.onCreate(savedInstanceState);
 mapFragment.onResume();
 mapFragment.getMapAsync(this);
 View mapView = mapFragment.getRootView();
 super.onViewCreated(mapView , savedInstanceState);

} }

Replace Your MapsFragment with this.用这个替换你的 MapsFragment。

public class MapsFragment extends Fragment implements
    OnMapReadyCallback {

  Context context;


public MapsFragment() {
    // Required empty public constructor
    super();
}

    @Override
public void onAttach(Context context) {
    super.onAttach(context);
    this.context = context;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_map, container, false);
}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    FrameLayout map_frame = view.findViewById(R.id.map_frame);

    // configure map
    MapView mapFragment = view.findViewById(R.id.map_view);
    mapFragment.onCreate(savedInstanceState);
    mapFragment.onResume();
    mapFragment.getMapAsync(this);
    View mapView = mapFragment.getRootView();
    super.onViewCreated(view, savedInstanceState);
}

@Override
public void onResume() {
    super.onResume();
    Log.i(LOG_TAG, "MapFragment resumed");
}


@Override
public void onPause() {
    super.onPause();
    Log.i(LOG_TAG, "Map fragment paused");
}

@Override
public void onMapReady(GoogleMap googleMap) {
    Log.i(LOG_TAG, "onMapReadyCalled");
    MapsInitializer.initialize(context);
}

} }

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

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