使用viewPager

时间:2017-12-03 17:57:20

标签: java android android-fragments runtime-error placeautocompletefragment

崩溃是指我将页面从2切换为0或1。 当我在activity_maps.xml

中评论自动完成片段时

请帮帮我。

  

---------崩溃的开始
  12-04 01:31:01.005 12693-12693 / com.handsomelee.gotroute E / AndroidRuntime:FATAL EXCEPTION:main                                                                             过程:com.handsomelee.gotroute,PID:12693                                                                             android.view.InflateException:二进制XML文件行#10:二进制XML文件行#10:错误膨胀类片段                                                                             引起:android.view.InflateException:二进制XML文件行#10:错误膨胀类片段                                                                             引起:java.lang.IllegalArgumentException:二进制XML文件行#10:复制id 0x7f0f00e4,标记null或父id 0xffffffff与com.google.android.gms.location.places.ui.PlaceAutocompleteFragment的另一个片段                                                                                 在android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2315)                                                                                 在android.app.FragmentController.onCreateView(FragmentController.java:98)                                                                                 在android.app.Activity.onCreateView(Activity.java:5901)                                                                                 在android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41)                                                                                 在android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67)                                                                                 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)                                                                                 在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)                                                                                 在android.view.LayoutInflater.rInflate(LayoutInflater.java:858)                                                                                 在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)                                                                                 在android.view.LayoutInflater.inflate(LayoutInflater.java:518)                                                                                 在android.view.LayoutInflater.inflate(LayoutInflater.java:426)                                                                                 在com.handsomelee.gotroute.Services.GoogleMapSystem.onCreateView(GoogleMapSystem.java:41)                                                                                 在android.support.v4.app.Fragment.performCreateView(Fragment.java:2261)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)                                                                                 在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750)                                                                                 在android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:792)                                                                                 在android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590)                                                                                 在android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377)                                                                                 在android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332)                                                                                 在android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209)                                                                                 在android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649)                                                                                 在android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145)                                                                                 在android.support.v4.view.ViewPager.populate(ViewPager.java:1238)                                                                                 在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663)                                                                                 在android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625)                                                                                 在android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617)                                                                                 at com.handsomelee.gotroute.MainActivity $ 1.onTabSelected(MainActivity.java:73)                                                                                 在android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165)                                                                                 在android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158)                                                                                 在android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128)                                                                                 在android.support.design.widget.TabLayout $ Tab.select(TabLayout.java:1427)                                                                                 在android.support.design.widget.TabLayout $ TabView.performClick(TabLayout.java:1537)                                                                                 在android.view.View $ PerformClick.run(View.java:22429)                                                                                 在android.os.Handler.handleCallback(Handler.java:751)                                                                                 在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                 在android.os.Looper.loop(Looper.java:154)                                                                                 在android.app.ActivityThread.main(ActivityThread.java:6119)                                                                                 at java.lang.reflect.Method.invoke(Native Method)                                                                                 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:886)                                                                                 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)                                                                        
---------系统的开头

这是我的MainActivity.java

@Override
public Fragment getItem(int position) {
  switch (position) {
    case 0:
      if(tab1 == null){
        tab1 = new MapsActivity(R.id.mapView, R.layout.activity_maps, GoogleMap.MAP_TYPE_NORMAL);
      }
      return tab1;

    case 1:
      if(tab2 == null)
      tab2 = new CarParkingActivity();
      return tab2;

    case 2:
      if(tab3 == null)
      tab3 = new ReportActivity();
      return tab3;

    default:
      return null;
  }

}

@Override
public int getCount() {
  return 3;
}

MapsActivity.java

@Override
  public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle bundle) {
  rootView = layoutInflater.inflate(layoutActivityId, viewGroup, false);
  mapView = ((MapView) rootView.findViewById(mapViewId));
  mapView.onCreate(bundle);
  mapView.getMapAsync(this);
  addOn();
  return rootView;
}
@Override
  public void onMapReady(GoogleMap googleMap) {
  mMap = googleMap;
  mMap.setMapType(googleMapType);
  mMap.setOnMyLocationButtonClickListener(this);
  mMap.setOnMyLocationClickListener(this);
  mMap.setOnMapLongClickListener(this);
}
}

activity_maps.xml

<?xml version="1.0" encoding="utf-8"?>
      <android.support.constraint.ConstraintLayout
              xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              tools:context="com.handsomelee.gotroute.Controller.ReportActivity">

        <fragment
                android:layout_width="0dp"
                android:layout_height="50dp"
                android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
                android:id="@+id/placeSearch"
                android:layout_weight="0.67"
                app:layout_constraintTop_toTopOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"/>

        <com.google.android.gms.maps.MapView
                android:layout_width="0dp"
                android:layout_height="0dp"
                android:id="@+id/mapView"
                android:layout_weight="0.78"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintBottom_toBottomOf="parent"
                android:apiKey="@string/test_google_api"
                app:layout_constraintHorizontal_bias="0.0"
                app:layout_constraintTop_toBottomOf="@+id/placeSearch"/>
      </android.support.constraint.ConstraintLayout>

ReportActivity.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
  View rootView = inflater.inflate(R.layout.activity_report, container, false);

  spinner = (Spinner) rootView.findViewById(R.id.spinner);
  String[] array_Spiner = getResources().getStringArray(R.array.reports_array);
  ArrayAdapter<String> adapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, array_Spiner);

  spinner.setAdapter(adapter);
  spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    ~
  });

  return rootView;
}

activity_report.xml

<?xml version="1.0" encoding="utf-8"?>`
    <android.support.constraint.ConstraintLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:tools="http://schemas.android.com/tools"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            tools:context="com.handsomelee.gotroute.Controller.ReportActivity">

      <Spinner
              android:layout_width="0dp"
              android:layout_height="50dp"
              android:id="@+id/spinner"
              app:layout_constraintTop_toTopOf="parent"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              android:overlapAnchor="false"
              android:dropDownWidth="match_parent"
              app:layout_constraintHorizontal_bias="0.0"/>

    </android.support.constraint.ConstraintLayout>

1 个答案:

答案 0 :(得分:0)

解决。
问题似乎是创建视图时片段ID重复 所以我在MapActivity中添加下面的代码来删除它。

@Override
  public void onDestroyView() {
    super.onDestroyView();
    android.app.FragmentManager fm = getActivity().getFragmentManager();
    android.app.Fragment fragment = fm.findFragmentById(R.id.placeSearch);
    android.app.FragmentTransaction ft = fm.beginTransaction();
    ft.remove(fragment);
    ft.commit();
  }