no_profile
no_profile

Reputation: 374

Why is my RecyclerView not showing in Fragment?

I am new in using RecyclerView so I just followed my previous working code so that I can apply this on a thesis project that I am doing.

The problem is that when I click my NavDrawer and select the option to show my Inventory Fragment where my RecyclerView is stored, my app will suddenly close.

There is an error in my logcat but I don't know why there is an error since my previous project contains the same line of code.

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>

Edit:

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>

Upvotes: 0

Views: 479

Answers (2)

pz64_
pz64_

Reputation: 2252

Update your Adapter as follows.

Inflate customlayout_inventorylist instead of fragment_list_options

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

Here is your updated adapter.

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

Upvotes: 2

sagar sonar
sagar sonar

Reputation: 46

Instead of

<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>

Use Framelayout and replace your current fragment to inventory list fragment

Upvotes: 0

Related Questions