0

I was trying to implement a mapview in a fragment with viewpager but despite I tried many solutions, I couldn't find the correct one for my project.

Here is my logcat

2019-05-26 17:28:16.645 12469-12469/com.example.emrullah.booksharingappv4 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.emrullah.booksharingappv4, PID: 12469
    android.view.InflateException: Binary XML file line #6: Binary XML file line #6: Error inflating class com.google.android.gms.maps.MapView
    Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class com.google.android.gms.maps.MapView
    Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:334)
        at android.view.LayoutInflater.createView(LayoutInflater.java:651)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:794)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:734)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:867)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:828)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:519)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:427)
        at com.example.emrullah.booksharingappv4.Fragments.MapviewFragment.onCreateView(MapviewFragment.java:50)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:2439)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1460)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:802)
        at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2625)
        at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2411)
        at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2366)
        at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:2243)
        at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:654)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:146)

here is my fragment

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.mapview_fragment, container, false);

        mLocationList= new ArrayList<>();
        mLocList= new ArrayList<>();
        mLocList2= new ArrayList<>();
        mRef=FirebaseDatabase.getInstance().getReference().child("users");
        currentUser=FirebaseAuth.getInstance().getCurrentUser();
        getLatLongFromDb();
        split(mLocList);

        mMapView = view.findViewById(R.id.map_view);
        mMapView.onCreate(savedInstanceState);

        mMapView.onResume();

        try {
            MapsInitializer.initialize(getActivity().getApplicationContext());
        } catch (Exception e) {
            e.printStackTrace();
        }

        //Gets the Googlemap from Mapview and does the initialization stuff
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mMap = googleMap;
                mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);


                for (int i = 0;i<mLocList2.size();i++){
                    LatLng sydney = new LatLng(mLocList2.get(i),mLocList2.get(i+1));
                    mMap.addMarker(new MarkerOptions().position(sydney).title("Book dest:"+(i+1)));
                }
            }
        });


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

        if (mMapView != null)
            mMapView.onResume();
    }


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

        if (mMapView != null)
            mMapView.onDestroy();
    }


    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        if (mMapView != null)
            mMapView.onSaveInstanceState(outState);
    }
}

Here is my xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.gms.maps.MapView
        android:id="@+id/map_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.example.emrullah.booksharingappv4.Fragments.MapviewFragment"
        />
</RelativeLayout>

And viewPager part in the Activity

public void setupViewPager(ViewPager viewPager) {
        SectionPageAdapter sectionPageAdapter = new SectionPageAdapter(getSupportFragmentManager());
        sectionPageAdapter.addFragment(new BookListFragment(), "BOOKS");
        sectionPageAdapter.addFragment(new MapviewFragment(), "BOOK MAP");
        sectionPageAdapter.addFragment(new SettingsFragment(), "SETTINGS");
        viewPager.setAdapter(sectionPageAdapter);
    }
    public void setUpTabIcons(){
        tabLayout.getTabAt(0).setIcon(navIcons[0]);
        tabLayout.getTabAt(1).setIcon(navIcons[1]);
        tabLayout.getTabAt(2).setIcon(navIcons[2]);
    }

I know there are some similar questions like mine but I tried all of them and none of them worked. Thanks for the help.

Xarybdis
  • 119
  • 5

1 Answers1

0

You are attempting to use the original version of Google Maps support for Android (com.google.android.maps). We refer to that now as Maps V1.

This will not work, as Maps V1 has been deprecated for over 4 years. You can no longer get API keys for it, so even if your code would be correct, it would not work. The crash is because the old MapView had to be used inside of a MapActivity, as the error indicates.

The current version of Google Maps support for Android — Maps V2 — has a very different API and does not involve classes in the com.google.android.maps package.

please read how to use Maps V2 form the official documentation

ismail alaoui
  • 5,440
  • 2
  • 17
  • 35
  • Thanks for it, you are right about the version but when I correct the version then viewpager gives error because the expected type is Fragment and I am trying to pass FragmentActivity. I couldnt find a way to convert it. Do you have any suggestion for it as well?? – Xarybdis May 26 '19 at 18:30
  • your welcome , please consider my answer if it's correct , and for viewpager you have to work with fragment – ismail alaoui May 26 '19 at 18:32
  • I already gave vote but because of I dont have enough reputation it just saved. And yeah I have to work with fragment but isnt there any way to convert FragmentActivity to Fragment ? – Xarybdis May 26 '19 at 18:45
  • and also mark it as correct , because it the correct answer for your initial question ! , you can't transform fragmentActivity to fragment , please read this : https://stackoverflow.com/questions/10609268/what-is-the-difference-between-fragment-and-fragmentactivity – ismail alaoui May 26 '19 at 18:49