在Recycler视图适配器上膨胀任何视图时出错

时间:2016-03-11 21:25:40

标签: android android-recyclerview

我的主要布局包含View PagerTabLayoutView Pager的每个页面都是Fragment,其中一个片段有RecyclerView

我的问题是,在我的onCreateViewHolder我的适配器上,并没有夸大任何视图来创建我的ViewHolder。即使只是一个简单的按钮,我也会遇到异常。

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="8dp"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/main_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

        <android.support.design.widget.TabLayout
            android:id="@+id/main_tablayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </android.support.design.widget.AppBarLayout>

    <android.support.v4.view.ViewPager
        android:id="@+id/main_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.design.widget.CoordinatorLayout>

fragment.xml

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

    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycler_produtos"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical">

    </android.support.v7.widget.RecyclerView>
</FrameLayout>

card_produtos.xml

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

    <Button
        android:id="@+id/nome"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/activity_horizontal_margin"
        android:elevation="?attr/cardElevation"
        android:gravity="center"
        android:padding="@dimen/activity_vertical_margin">


    </Button>
</LinearLayout>

Adapter.java

public class ProdutosAdapter extends RecyclerView.Adapter<ProdutosAdapter.ViewHolder> {
        private List<Produto> produtos;
        private int idLayout;

        public ProdutosAdapter(List<Produto> produtos, int idLayout) {
            this.produtos = produtos;
            this.idLayout = idLayout;
        }

        @Override
        public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            //error below
            View v = LayoutInflater.from(parent.getContext()).inflate(idLayout, parent, false);
            return new ViewHolder(v);
        }

        @Override
        public void onBindViewHolder(ViewHolder holder, int position) {
            Produto produto = produtos.get(position);
            holder.mNome.setText(produto.getProduto());
        }

        @Override
        public int getItemCount() {
            return produtos.size();
        }

        public class ViewHolder extends RecyclerView.ViewHolder {

            private final TextView mNome;

            public ViewHolder(View itemView) {
                super(itemView);
                mNome = (TextView) itemView.findViewById(R.id.nome);
            }
        }

        public void update(List<Produto> produtos) {
            this.produtos.clear();
            this.produtos.addAll(produtos);
            notifyDataSetChanged();
        }
    }

Fragment.java

@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_produtos, container, false);

        RecyclerView mRecycler = (RecyclerView) rootView.findViewById(R.id.recycler_produtos);
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        mRecycler.setLayoutManager(manager);
        mProdutosAdapter = new ProdutosAdapter(new ArrayList<Produto>(), R.layout.card_produtos);
        mRecycler.setAdapter(mProdutosAdapter);

        return rootView;
    }

logcat

03-11 19:03:37.379 9433-9433/br.com.orangesistemas.comanda2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: br.com.orangesistemas.comanda2, PID: 9433
                                                                              android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
                                                                                  at android.view.LayoutInflater.createView(LayoutInflater.java:640)
                                                                                  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:750)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:483)
                                                                                  at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
                                                                                  at br.com.orangesistemas.comanda2.adapter.ProdutosAdapter.onCreateViewHolder(ProdutosAdapter.java:27)
                                                                                  at br.com.orangesistemas.comanda2.adapter.ProdutosAdapter.onCreateViewHolder(ProdutosAdapter.java:16)
                                                                                  at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:5223)
                                                                                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4449)
                                                                                  at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4359)
                                                                                  at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
                                                                                  at android.support.v7.widget.GridLayoutManager.layoutChunk(GridLayoutManager.java:438)
                                                                                  at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
                                                                                  at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
                                                                                  at android.support.v7.widget.GridLayoutManager.onLayoutChildren(GridLayoutManager.java:171)
                                                                                  at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
                                                                                  at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
                                                                                  at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
                                                                                  at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
                                                                                  at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1133)
                                                                                  at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
                                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
                                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1702)
                                                                                  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1556)
                                                                                  at android.widget.LinearLayout.onLayout(LinearLayout.java:1465)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
                                                                                  at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
                                                                                  at android.view.View.layout(View.java:16942)
                                                                                  at android.view.ViewGroup.layout(ViewGroup.java:5409)
                                                                                  at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2457)
                                                                                at android.view.ViewRootImpl.perform

0 个答案:

没有答案