为什么我的RecyclerView没有显示在片段中?

时间:2019-07-06 08:20:32

标签: android android-recyclerview

我是RecyclerView的新手,所以我只是遵循以前的工作代码,因此可以将其应用到我正在做的论文项目中。

问题是,当我单击我的NavDrawer并选择显示存储我的RecyclerView的清单片段的选项时,我的应用会突然关闭。

我的logcat中有一个错误,但是我不知道为什么有错误,因为我的上一个项目包含相同的代码行。

logcat

2019-07-06 16:07:22.963 6623-6623/com.example.devcash E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.devcash, PID: 6623
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
        at com.example.devcash.CustomAdapters.InventoryOptionsAdapter$RecyclerViewHolder.bindView(InventoryOptionsAdapter.java:45)
        at com.example.devcash.CustomAdapters.InventoryOptionsAdapter.onBindViewHolder(InventoryOptionsAdapter.java:24)
        at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
        at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
        at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
        at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
        at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
        at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
        at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
        at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)

InventoryOptionsAdapter.java

public class InventoryOptionsAdapter extends RecyclerView.Adapter {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_list_options, viewGroup, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((RecyclerViewHolder)viewHolder).bindView(i);
    }

    @Override
    public int getItemCount() {
        return InventoryOptions.label.length;
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mItemText;
        private ImageView mItemImage;

        public RecyclerViewHolder(View itemView){
            super(itemView);
            mItemText = (TextView) itemView.findViewById(R.id.inventorylist_title);
            mItemImage = (ImageView) itemView.findViewById(R.id.inventorylist_icon);
            itemView.setOnClickListener(this);
        }


        public void bindView(int position){
            mItemText.setText(InventoryOptions.label[position]);
            mItemImage.setImageResource(InventoryOptions.picturePath[position]);
        }

        @Override
        public void onClick(View v) {
            //write something here
        }
    }
}

ListOptionFragment.java

public class ListOptionsFragment extends Fragment {


    public ListOptionsFragment() {
        // Required empty public constructor
    }


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

        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerview_inventorylist);

        InventoryOptionsAdapter adapter = new InventoryOptionsAdapter();
        recyclerView.setAdapter(adapter);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getActivity());
        recyclerView.setLayoutManager(layoutManager);

        return view;
    }

}

fragment_inventory.xml

<LinearLayout 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"
    tools:context=".Fragments.InventoryFragment"
    android:orientation="horizontal"
    android:id="@+id/inventory_content">

    <fragment
        android:id="@+id/inventorylist_fragment"
        android:name="com.example.devcash.ListOptionsFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_list_options">

    </fragment>

    <View
        style="@style/Divider"
        android:layout_width="1dp"
        android:layout_height="wrap_content" />

    <RelativeLayout
        android:id="@+id/inventorylist_fragmentcontainer"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="2"/>


</LinearLayout>

编辑:

fragment_list_options.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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".ListOptionsFragment">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar_purchaseitemlist"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        style="@style/PrimaryHeaderBar"
        android:elevation="4dp"
        app:title="@string/inventory_title"
        app:titleTextAppearance="@style/TextAppearance.AppCompat.Title"/>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerview_inventorylist"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:choiceMode="singleChoice"
        android:listSelector="@color/light_gray"/>
</FrameLayout>

customlayout_inventorylist.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="@dimen/padding_20"
    android:gravity="left|center">


    <ImageView
        android:id="@+id/inventorylist_icon"
        android:layout_width="30dp"
        android:layout_height="30dp"
        android:src="@drawable/ic_email"
        android:tint="@color/icon_light"/>

    <TextView
        android:id="@+id/inventorylist_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/inventory_title"
        android:paddingLeft="@dimen/padding_50"
        android:textSize="@dimen/page_title"
        android:textColor="@color/dark_text"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:2)

如下更新您的适配器。

customlayout_inventorylist而不是fragment_list_options充气

        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customlayout_inventorylist, viewGroup, false);

这是您更新的适配器。

public class InventoryOptionsAdapter extends RecyclerView.Adapter {
    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.customlayout_inventorylist, viewGroup, false);
        return new RecyclerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {
        ((RecyclerViewHolder)viewHolder).bindView(i);
    }

    @Override
    public int getItemCount() {
        return InventoryOptions.label.length;
    }

    private class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private TextView mItemText;
        private ImageView mItemImage;

        public RecyclerViewHolder(View itemView){
            super(itemView);
            mItemText = (TextView) itemView.findViewById(R.id.inventorylist_title);
            mItemImage = (ImageView) itemView.findViewById(R.id.inventorylist_icon);
            itemView.setOnClickListener(this);
        }


        public void bindView(int position){
            mItemText.setText(InventoryOptions.label[position]);
            mItemImage.setImageResource(InventoryOptions.picturePath[position]);
        }

        @Override
        public void onClick(View v) {
            //write something here
        }
    }
}

答案 1 :(得分:0)

代替

<fragment
        android:id="@+id/inventorylist_fragment"
        android:name="com.example.devcash.ListOptionsFragment"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:layout_weight="1"
        tools:layout="@layout/fragment_list_options">

    </fragment>

使用Framelayout并将当前片段替换为清单列表片段