Google Map Lite in CardView

Duncan Roosma picture Duncan Roosma · Dec 11, 2014 · Viewed 7.7k times · Source

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);
    }
}

Answer

Simone picture Simone · Dec 17, 2014

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