CardView中的Google Map Lite

时间:2014-12-11 11:02:59

标签: android google-maps google-play-services android-recyclerview android-viewholder

我一直试图让一个非常基本的应用程序去玩Material Design。 我正在尝试创建一个Listview(使用新的RecyclerView),每个项目都是一个卡片女巫有一个Google Maps Lite。

我基本上一直关注此示例:Creating Lists and Cards,对于我一直使用的Google地图实施this example

当我在适配器中只有一个项目时,每件事都“工作”(所以只有一个项目要显示) 但是当我向适配器添加第二个项目(因此列表中应该有两个项目)时,应用程序崩溃,但异常:

  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.
是谁可以告诉我我做错了什么?

除了错误之外,我似乎无法弄清楚在各个地图(中心和东西)上设置属性的最佳方法。在这方面的帮助也将非常感激。

实施参考:

活动

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

2 个答案:

答案 0 :(得分:14)

尝试使用MapView而不是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);

}

请务必在OnMapReadyCallback中实施RecyclerView.Adapter

西蒙

答案 1 :(得分:1)

尝试使用新的MapFragment()等在运行时添加片段。当从同一布局中不止一次地膨胀地图片段时,我经常遇到这个错误的麻烦。奇怪的问题......