在Fragment中使用Google地图时,应用程序崩溃了

时间:2014-03-26 10:09:21

标签: android google-maps android-fragments

我第一次使用Fragments作为我的应用程序。我已经在其中一个片段中实现了Google Map。每当我选择带有地图的标签时,它会成功加载,但是一旦我在其他活动中进入主菜单,它会崩溃,我不知道为什么。

这是我的崩溃日志: -

03-28 14:14:23.175: E/AndroidRuntime(18215): FATAL EXCEPTION: main
03-28 14:14:23.175: E/AndroidRuntime(18215): java.lang.RuntimeException: Unable to destroy activity {MyFragmentActivity}: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3444)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3462)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread.access$1200(ActivityThread.java:149)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1358)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.os.Looper.loop(Looper.java:153)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread.main(ActivityThread.java:5000)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at java.lang.reflect.Method.invokeNative(Native Method)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at java.lang.reflect.Method.invoke(Method.java:511)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at dalvik.system.NativeStart.main(Native Method)
03-28 14:14:23.175: E/AndroidRuntime(18215): Caused by: java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1343)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1361)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at com.fragments.FindMyCar.onDestroyView(FindMyCar.java:297)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.Fragment.performDestroyView(Fragment.java:1705)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:996)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1086)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentManagerImpl.dispatchDestroy(FragmentManager.java:1921)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.support.v4.app.FragmentActivity.onDestroy(FragmentActivity.java:335)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.Activity.performDestroy(Activity.java:5184)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1111)
03-28 14:14:23.175: E/AndroidRuntime(18215):    at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3431)
03-28 14:14:23.175: E/AndroidRuntime(18215):    ... 11 more

在我的片段中,我实现了这样的: -

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.findmycar, null);

        initViews(view);

        // Getting Google Play availability status
        int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getActivity());


        if(status!=ConnectionResult.SUCCESS){ // Google Play Services are not available

            int requestCode = 10;
            Dialog dialog = GooglePlayServicesUtil.getErrorDialog(status, getActivity(), requestCode);
            dialog.show();

        }else { // Google Play Services are available

            fm = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.map);

            map = fm.getMap();

            if(map == null){

                //Toast.makeText(getActivity(), "null", 0).show();
            }
            else{

                map.setMyLocationEnabled(true);
                // checking for the best provider and connectivity

                // Getting LocationManager object from System Service LOCATION_SERVICE
                locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);

                if (netInfo !=null && netInfo.isConnected()) {


                    if(isGpsEnable()){


                        provider=LocationManager.GPS_PROVIDER;
                        mLocation = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

                        if(mLocation==null){
                            //onLocationChanged(location);

                            provider=LocationManager.NETWORK_PROVIDER;
                            mLocation = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                            Log.d("Location","Location " + mLocation + " " + provider);

                            if(mLocation!=null){

                                onLocationChanged(mLocation);
                            }
                            else{

                                Toast.makeText(getActivity(), "Unable to fetch your current location at the moment. Please try again later.", Toast.LENGTH_LONG).show();
                            }

                        }else {
                            onLocationChanged(mLocation);
                        }

                    }
                    else{

                        showGpsAlert();
                    }

                }
                else{

                    Toast.makeText(getActivity(), "No internet connection", Toast.LENGTH_LONG).show();
                }



            }
        }


@Override
    public void onDestroyView() {
        super.onDestroyView();
        Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));  
        if(fragment!=null){
            Toast.makeText(getActivity(), "inif", 0).show();
            FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
            ft.remove(fragment);
            ft.commit();
        }
        else{


        }

    }


    @Override
    public void onDestroy() {
        super.onDestroy();
    }

    @Override
    public void onResume() {
        super.onResume();
        }
    }

它在onDestroyView()方法中销毁引用时完美运行但在该应用程序崩溃后,不知道该怎么做。

任何帮助都会很明显。 提前谢谢。

1 个答案:

答案 0 :(得分:6)

试试这个,让我知道将以下代码放入onDestroyView()

try{
          SupportMapFragment fragment = ((SupportMapFragment) getFragmentManager().findFragmentById(R.id.map));
          FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
          ft.remove(fragment);
          ft.commit();
        }catch(Exception e){
        }