0

I get a Error inflating class fragment when using the google maps api and displaying it on a fragment. It gets displayed in a swipe tab, but that should not be the problem. Here is my code:

Activity:

public class MapActivity extends Fragment implements OnMapReadyCallback  {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.activity_map, container, false);
        return view;
    }

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

    @Override
    public void onMapReady(GoogleMap map) {
        LatLng sydney = new LatLng(-33.867, 151.206);

        map.setMyLocationEnabled(true);
        map.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));

        map.addMarker(new MarkerOptions()
                .title("Sydney")
                .snippet("The most populous city in Australia.")
                .position(sydney));
    }
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <fragment
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:name="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

Stacktrace:

android.view.InflateException: Binary XML file line #7: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
        at com.example.MapActivity.onCreateView(MapActivity.java:15)
        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
        at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:163)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1835)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:725)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:616)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5802)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2881)
        at android.view.View.measure(View.java:18425)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2237)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1295)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1537)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1180)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6558)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
        at android.view.Choreographer.doCallbacks(Choreographer.java:590)
        at android.view.Choreographer.doFrame(Choreographer.java:560)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:145)
        at android.app.ActivityThread.main(ActivityThread.java:5834)
        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:1388)

The line it shows the error is the following one: View view = inflater.inflate(R.layout.activity_map, container, false);

Broadwell
  • 1,343
  • 2
  • 19
  • 33

1 Answers1

0

You Activity class extends Fragment, that means it's a Fragment not an Activity.

You should extends FragmentActivity.

Also for the sample map code, you can create a MapActivity in Android Studio as following:

enter image description here

And then you can put your map set up code in the Activity.

EDIT

If you wanna make a fragment of map, and use something like tabs to switch. And I tried your code, no problem with it, I think, you need to check out the name of fragment layout you inflate into. And also change the getFragmentManager to getChildFragmentManager.

EDIT

For your code, it will surely encounter the inflater problem when I am switching tabs, and I tried code here, it works, is it ok for you to change a little of you code? if so, you can try that.

I uploaded working code here. I did the map fragment in one of my project. Just check out 3rd tab named UpcomingFrament, it includes Map fragment

Community
  • 1
  • 1
bjiang
  • 6,068
  • 2
  • 22
  • 35
  • But I still want it to be swipeable afterwards, which I don't think is possible with FragmentActivity. Also it doesn't solve the problem with creating a new instance of the fragment every time you switch to it – Broadwell May 18 '15 at 19:08
  • I see what your mean, you wanna make a `fragment` of map, and use something like `tabs` to switch. And I tried your code, no problem with it, I think, you need to check out the `name` of fragment and `layout` you inflate into. And also change the `getFragmentManager` to `getChildFragmentManager`. – bjiang May 18 '15 at 19:35
  • I can upload the success sample code I tried if you want – bjiang May 18 '15 at 19:49
  • That woukd be anazing :) – Broadwell May 18 '15 at 19:50
  • Also, the map is also a fragment – Broadwell May 18 '15 at 19:50
  • For your code, it will surely encounter the inflater problem when I am switching tabs, and I tried code [here](http://stackoverflow.com/questions/20919048/android-android-view-inflateexception-binary-xml-file-line-8-error-inflatin), it works, is it ok for you to change a little of you code? if so, you can try that. – bjiang May 18 '15 at 20:13
  • I uploaded working code [here](https://github.com/jbj88817/MapTab-android/tree/master). I did the `map fragment` in one of my project. Just check out 3rd tab named UpcomingFrament, it includes Map fragment – bjiang May 18 '15 at 20:24
  • Thank you very much, will check it out asap – Broadwell May 19 '15 at 05:54
  • Please tell me if you have problems – bjiang May 26 '15 at 16:13