-2

The crash is when i switch the page from 2 to 0 or 1. And when i Comment the auto Complete Fragment at activity_maps.xml

Please help me.

--------- beginning of crash
12-04 01:31:01.005 12693-12693/com.handsomelee.gotroute E/AndroidRuntime: FATAL EXCEPTION: main Process: com.handsomelee.gotroute, PID: 12693 android.view.InflateException: Binary XML file line #10: Binary XML file line #10: Error inflating class fragment Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class fragment Caused by: java.lang.IllegalArgumentException: Binary XML file line #10: Duplicate id 0x7f0f00e4, tag null, or parent id 0xffffffff with another fragment for com.google.android.gms.location.places.ui.PlaceAutocompleteFragment at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2315) at android.app.FragmentController.onCreateView(FragmentController.java:98) at android.app.Activity.onCreateView(Activity.java:5901) at android.support.v4.app.BaseFragmentActivityApi14.onCreateView(BaseFragmentActivityApi14.java:41) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:67) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777) 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.handsomelee.gotroute.Services.GoogleMapSystem.onCreateView(GoogleMapSystem.java:41) at android.support.v4.app.Fragment.performCreateView(Fragment.java:2261) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1750) at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:792) at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2590) at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2377) at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2332) at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2209) at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:649) at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:145) at android.support.v4.view.ViewPager.populate(ViewPager.java:1238) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:663) at android.support.v4.view.ViewPager.setCurrentItemInternal(ViewPager.java:625) at android.support.v4.view.ViewPager.setCurrentItem(ViewPager.java:617) at com.handsomelee.gotroute.MainActivity$1.onTabSelected(MainActivity.java:73) at android.support.design.widget.TabLayout.dispatchTabSelected(TabLayout.java:1165) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1158) at android.support.design.widget.TabLayout.selectTab(TabLayout.java:1128) at android.support.design.widget.TabLayout$Tab.select(TabLayout.java:1427) at android.support.design.widget.TabLayout$TabView.performClick(TabLayout.java:1537) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
--------- beginning of system

this is my 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>
Gibson Kong
  • 1
  • 1
  • 1
  • Avoid questions with too many lines of code. Questions asking us to debug your code are considered off-topic. Consider reading [How to create a Minimal, Complete, and Verifiable example](https://stackoverflow.com/help/mcve). – Mateus Dec 03 '17 at 18:01
  • Refer to [this question's answer.](https://stackoverflow.com/a/19815266/7970936) It appears to be an issue with your mapview. – Danny Michelin Dec 03 '17 at 18:18
  • @DannyMichelin But When i remove the mapview. it also got the problem. – Gibson Kong Dec 04 '17 at 00:13
  • @Mateus thans for your advise. I had updated my post already – Gibson Kong Dec 04 '17 at 00:24

1 Answers1

0

SOLVED.
The problem seems like is the fragment id duplicated when the view is created.
so I add the Code below at my MapActivity to remove it.

@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();
  }
Gibson Kong
  • 1
  • 1
  • 1