在recyclerView.setHasFixedSize(true)上获取空指针异常

时间:2018-09-04 19:34:23

标签: android android-studio android-fragments android-recyclerview

在我的应用程序中打开片段时遇到此异常。 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)'

日志详细信息

                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
                  at com.hotwings.truekitchen.TabCookMenu.onCreateView(TabCookMenu.java:61)
                  at android.app.Fragment.performCreateView(Fragment.java:2508)
                  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1279)
                  at android.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2407)
                  at android.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2186)
                  at android.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2142)
                  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2043)

下面是我的片段代码

    import android.app.Fragment;
    import android.os.Bundle;
    import android.support.v7.widget.LinearLayoutManager;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.LinearLayout;

    import java.util.ArrayList;
    import java.util.List;



    public class TabCookMenu extends Fragment   {

        public static TabCookMenu newInstance() {

            return new TabCookMenu();
        }

        LinearLayout linearLayout;
        View rootView;



        private String url = "http://192.168.43.196/Volley/data.json";

       List<CookMenuList> productList;

        //the recyclerview
        RecyclerView recyclerView;

    //
    //    @Override
    //    public View onCreateView(LayoutInflater inflater, ViewGroup container,


        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            super.onActivityCreated(savedInstanceState);
            // Inflate the layout for this fragment
             rootView = inflater.inflate(R.layout.singlecookmenulist, container, false);

            //getting the recyclerview from xml
            recyclerView =  (RecyclerView) rootView.findViewById(R.id.recyclerView);

          recyclerView.setHasFixedSize(true);




            productList = new ArrayList<>();
            //adding some items to our list
            productList.add(
                    new CookMenuList(
                            1,
                            "Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)",
                            "13.3 inch, Silver, 1.35 kg",
                            4.3,
                            60000,
                            R.drawable.ic_drawer));

            productList.add(
                    new CookMenuList(
                            1,
                            "Dell Inspiron 7000 Core i5 7th Gen - (8 GB/1 TB HDD/Windows 10 Home)",
                            "14 inch, Gray, 1.659 kg",
                            4.3,
                            60000,
                            R.drawable.ic_home));

            productList.add(
                    new CookMenuList(
                            1,
                            "Microsoft Surface Pro 4 Core m3 6th Gen - (4 GB/128 GB SSD/Windows 10)",
                            "13.3 inch, Silver, 1.35 kg",
                            4.3,
                            60000,
                            R.drawable.ic_trash));

            //creating recyclerview adapter


    //
            CookMenuAdaptor adapter = new CookMenuAdaptor(getActivity(), productList);


            //setting adapter to recyclerview
            recyclerView.setAdapter(adapter);
            LinearLayoutManager llm = new LinearLayoutManager(getActivity());
            llm.setOrientation(LinearLayoutManager.VERTICAL);
            recyclerView.setLayoutManager(llm);



            return rootView;

        }

    }

列出xml如下

这是我要在其中添加的清单XML

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="5dp">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="8dp">

                    <ImageView
                        android:id="@+id/imageView"
                        android:layout_width="120dp"
                        android:layout_height="90dp"
                        android:padding="4dp" />

                    <TextView
                        android:id="@+id/textViewTitle"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="5dp"
                        android:layout_toRightOf="@id/imageView"
                        android:text="Apple MacBook Air Core i5 5th Gen - (8 GB/128 GB SSD/Mac OS Sierra)"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small"
                        android:textColor="#000000" />

                    <TextView
                        android:id="@+id/textViewShortDesc"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/textViewTitle"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="5dp"
                        android:layout_toRightOf="@id/imageView"
                        android:text="13.3 Inch, 256 GB"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small" />

                    <TextView
                        android:id="@+id/textViewRating"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/textViewShortDesc"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="5dp"
                        android:layout_toRightOf="@id/imageView"
                        android:background="@color/colorPrimary"
                        android:paddingLeft="15dp"
                        android:paddingRight="15dp"
                        android:text="4.7"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Small.Inverse"
                        android:textStyle="bold" />

                    <TextView
                        android:id="@+id/textViewPrice"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_below="@id/textViewRating"
                        android:layout_marginLeft="5dp"
                        android:layout_marginTop="5dp"
                        android:layout_toRightOf="@id/imageView"
                        android:text="INR 56990"
                        android:textAppearance="@style/Base.TextAppearance.AppCompat.Large"
                        android:textStyle="bold" />

                </RelativeLayout>
            enter code here
            </android.support.v7.widget.CardView>

        </LinearLayout>

下面提到的回收者视图xml

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recyclerView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >
        </android.support.v7.widget.RecyclerView>
    </RelativeLayout>

适配器类

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;


public class CookMenuAdaptor extends RecyclerView.Adapter<CookMenuAdaptor.ProductViewHolder> {



    //this context we will use to inflate the layout
    private Context mCtx;

    //we are storing all the products in a list
    private List<CookMenuList> productList;

    //getting the context and product list with constructor
    public CookMenuAdaptor(Context mCtx, List<CookMenuList> productList) {
        this.mCtx = mCtx;
        this.productList = productList;
    }

    @Override
    public ProductViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        //inflating and returning our view holder
//        LayoutInflater inflater = LayoutInflater.from(mCtx);
//        View view = inflater.inflate(R.layout.singlecookmenulist, null);

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());

        View view = inflater.inflate(R.layout.singlecookmenulist, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ProductViewHolder holder, int position) {
        //getting the product of the specified position
        CookMenuList product = productList.get(position);

        //binding the data with the viewholder views
        holder.textViewTitle.setText(product.getTitle());
        holder.textViewShortDesc.setText(product.getShortdesc());
        holder.textViewRating.setText(String.valueOf(product.getRating()));
        holder.textViewPrice.setText(String.valueOf(product.getPrice()));

        holder.imageView.setImageDrawable(mCtx.getResources().getDrawable(product.getImage()));

    }


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


    class ProductViewHolder extends RecyclerView.ViewHolder {

        TextView textViewTitle, textViewShortDesc, textViewRating, textViewPrice;
        ImageView imageView;

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

            textViewTitle = itemView.findViewById(R.id.textViewTitle);
            textViewShortDesc = itemView.findViewById(R.id.textViewShortDesc);
            textViewRating = itemView.findViewById(R.id.textViewRating);
            textViewPrice = itemView.findViewById(R.id.textViewPrice);
            imageView = itemView.findViewById(R.id.imageView);
        }
    }
}

我尝试了各种选项并从包括stackoverflow在内的各种来源获得了帮助,但是我仍然遇到此问题。

0 个答案:

没有答案