将片段映射到另一个片段内会导致Activity崩溃

时间:2016-12-09 16:42:44

标签: android google-maps android-fragments

我正在使用Tabbed活动,在第一个片段中我正在使用地图片段。它工作正常,但问题是如果我在此活动中的其他片段之间进行更改,则整个活动崩溃。删除地图片段后,代码运行良好。但我想在片段中放置一个地图,所以请帮助。这是我的地图片段的xml代码

     <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="350dp"
    android:layout_height="200dp"
    android:layout_marginTop="20dp"
    android:id="@+id/map"
    android:layout_below="@+id/textView6"
    android:layout_alignParentLeft="true"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="19dp"
    android:layout_marginStart="19dp" 

每次活动崩溃前都是错误 -

    Process: com.gjs.finalproject, PID: 5571
                                             android.view.InflateException: Binary XML file line #30: Error inflating class fragment
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:767)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:810)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:508)
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:418)
                                                 at com.gjs.finalproject.HomeFragment.About2Fragment.onCreateView(About2Fragment.java:64)
                                                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080)
                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108)
                                                 at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1468)
                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:791)
                                                 at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638)
                                                 at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                                                 at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                 at android.support.v4.view.ViewPager$3.run(ViewPager.java:275)
                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:775)
                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:588)
                                                 at android.view.Choreographer.doFrame(Choreographer.java:557)
                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:761)
                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                 at android.os.Looper.loop(Looper.java:135)
                                                 at android.app.ActivityThread.main(ActivityThread.java:5276)
                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                 at java.lang.reflect.Method.invoke(Method.java:372)
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911)
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706)
                                              Caused by: java.lang.IllegalArgumentException: Binary XML file line #30: Duplicate id 0x7f0e009f, tag null, or parent id 0x7f0e009d with another fragment for com.google.android.gms.maps.SupportMapFragment
                                                 at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2422)
                                                 at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:44)
                                                 at android.view.LayoutInflater$FactoryMerger.onCreateView(LayoutInflater.java:177)
                                                 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:729)
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:810) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                                                 at android.view.LayoutInflater.rInflate(LayoutInflater.java:813) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:508) 
                                                 at android.view.LayoutInflater.inflate(LayoutInflater.java:418) 
                                                 at com.gjs.finalproject.HomeFragment.About2Fragment.onCreateView(About2Fragment.java:64) 
                                                 at android.support.v4.app.Fragment.performCreateView(Fragment.java:2080) 
                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1108) 
                                                 at android.support.v4.app.FragmentManagerImpl.attachFragment(FragmentManager.java:1468) 
                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:791) 
                                                 at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1638) 
                                                 at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679) 
                                                 at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143) 
                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1240) 
                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1088) 
                                                 at android.support.v4.view.ViewPager$3.run(ViewPager.java:275) 
                                                 at android.view.Choreographer$CallbackRecord.run(Choreographer.java:775) 
                                                 at android.view.Choreographer.doCallbacks(Choreographer.java:588) 
                                                 at android.view.Choreographer.doFrame(Choreographer.java:557) 
                                                 at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:761) 
                                                 at android.os.Handler.handleCallback(Handler.java:739) 
                                                 at android.os.Handler.dispatchMessage(Handler.java:95) 
                                                 at android.os.Looper.loop(Looper.java:135) 
                                                 at android.app.ActivityThread.main(ActivityThread.java:5276) 
                                                 at java.lang.reflect.Method.invoke(Native Method) 
                                                 at java.lang.reflect.Method.invoke(Method.java:372) 
                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:911) 
                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:706) 

这是我的片段代码

   @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
        mapFragment = ((SupportMapFragment) getChildFragmentManager()
                .findFragmentById(R.id.map));
      mapFragment.getMapAsync(this);
}


@Override
public void onDestroy() {
    super.onDestroy();
    if (mapFragment != null) {
        //mMap = null;
        mapFragment=null;
        }
    }

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    //Toast.makeText(getActivity(), lat + "   " + lng, Toast.LENGTH_LONG).show();

    LatLng locations = new LatLng(Util.reslatitude,Util.reslogitide);

    if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        return;
    }
    mMap.setMyLocationEnabled(true);
    mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locations, 13));

    mMap.addMarker(new MarkerOptions()
            .title("abc")

            .position(locations));
}

提前致谢

2 个答案:

答案 0 :(得分:1)

而不是在onActivityCreated初始化您的mapFragment onCreateView

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

          mapFragment = ((SupportMapFragment) getChildFragmentManager()
                    .findFragmentById(R.id.map));
          mapFragment.getMapAsync(this);
      }

您的onActivityCreated应该是

   @Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

添加以下代码,让我知道它是否有效

   @Override
   public void onPause() {
        super.onPause();

        getChildFragmentManager().beginTransaction()
                .remove(mapFragment)
                .commit();
    }

答案 1 :(得分:0)

而不是地图片段尝试在第一个片段中使用MapView