5

I have been trying to get a very basic app going to play around with Material Design. I am trying to create a Listview (using the new RecyclerView) in with every item is a Card witch has a Google Maps Lite in it.

I have basicly been following this example: Creating Lists and Cards, for the Google Maps implementation i have been using this example

Every thing "Works" when i have just one item in the adapter (so there is only one item to display) but when i add a second item to the adapter (so there should be two items in the list) the app crashes with the exception:

  21158-21158/lollipop.auxilium.nl.lollipoptest E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.view.InflateException: Binary XML file line #21: Error inflating class fragment
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:719)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:769)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:498)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
        at lollipop.auxilium.nl.lollipoptest.MyAdapter.onCreateViewHolder(MyAdapter.java:63)
        at lollipop.auxilium.nl.lollipoptest.MyAdapter.onCreateViewHolder(MyAdapter.java:25)
        at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
        at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
        at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
        at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1531)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1440)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
        at android.view.View.layout(View.java:15204)
        at android.view.ViewGroup.layout(ViewGroup.java:4793)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2260)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2007)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1249)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6364)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:791)
        at android.view.Choreographer.doCallbacks(Choreographer.java:591)
        at android.view.Choreographer.doFrame(Choreographer.java:561)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:777)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5419)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.

can any one tell me what i am doing wrong?

besides the error i can't seem to figure out what would be the best way to set properties on the individual maps (center and stuff). Help in that area would also be really appreciated.

Implementation for reference:

Activity

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

        mRecyclerView = (RecyclerView) rootView.findViewById(R.id.main_recycler_view);

        mLayoutManager = new LinearLayoutManager(context);
        mRecyclerView.setLayoutManager(mLayoutManager);

        ArrayList<String> data = new ArrayList<>();
        data.add("Toeter");

        mAdapter = new MyAdapter(data, getChildFragmentManager());
        mRecyclerView.setAdapter(mAdapter);

        return rootView;
    }

MyAdapter

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    final View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.card, parent, false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

ViewHolder

public static class ViewHolder extends RecyclerView.ViewHolder {
    public ViewHolder(View v) {
        super(v);
    }
}
Duncan Roosma
  • 481
  • 7
  • 21

2 Answers2

15

Try using MapView instead of MapFragment:

item.xml:

<android.support.v7.widget.CardView
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:cardCornerRadius="4dp">

        <com.google.android.gms.maps.MapView
                android:id="@+id/map_view"
                android:layout_width="match_parent"
                android:layout_height="148dp"

                app:liteMode="true"
                app:mapType="normal" />

</android.support.v7.widget.CardView>

RecyclerAdapter.java:

public ViewHolder(View itemView) {
   super(itemView);

   mapView = (MapView) itemView.findViewById(R.id.map_view);
}


@Override
    public void onBindViewHolder(ViewHolder holder, int position) {

                holder.mapView.onCreate(null);
                holder.mapView.getMapAsync(this);

}

Remember to implement OnMapReadyCallback in RecyclerView.Adapter.

Simone

Simone
  • 795
  • 2
  • 7
  • 17
  • I have tried that, and indeed there are two grid's displayed, but there is no map visible. CameraUpdateFactory also throws java.lang.NullPointerException: CameraUpdateFactory is not initialized in onMapReady(GoogleMap googleMap) – Duncan Roosma Dec 17 '14 at 12:08
  • If you have a only a grid with no map then you have to check the API KEY and the googlemap permissions. As regards CameraUpdateFactory try starting in onMapReady with MapsInitializer.initialize(context). – Simone Dec 18 '14 at 09:12
  • But when using fragment inside the cardview the map does display (when only one is drawn) so the settings for the Google maps itself is not wrong. – Duncan Roosma Dec 18 '14 at 09:13
  • Yes i used your example – Duncan Roosma Dec 18 '14 at 09:15
  • 1
    I've tried yesterday this solution in a RecyclerView and it worked. But then I went back to the "Static Map API" because in a RecyclerView in my opinion the MapsLite are still too heavy to be used (a lot of lag when scrolling). – Simone Dec 18 '14 at 09:27
  • I am getting grid in `MapView` too, but `MapFragment` is working fine. Has anybody found solution to that? – Eddwhis Jan 26 '15 at 08:59
  • @Eddwhis, I have your same problem: the Google Maps APIs work perfectly in MapFragments, but are not working with MapViews, as I only get grey grids. Did you find a solution? – Andrea Gottardo Feb 15 '15 at 19:49
  • I am facing same problem? did anyone found the solution? – A.s.ALI May 31 '19 at 04:28
1

Try adding the fragments at runtime, by using new MapFragment() and so on. I've been run into troubles with this error often, when inflating map fragment more than once from same layout. Weird issue...