skip to Main Content

Nowadays I am creating a android app. In my app I want to create a TextView as button in RecyclerView Item in fragment. So I use this code to implement it. but it does not work for me and error came.

This is my list_item_status_admin.xml file and this textview set as button.

 <TextView
                android:id="@+id/btn_approve_status"
                android:layout_width="85dp"
                android:layout_height="wrap_content"
                android:background="@drawable/black_shape"
                android:backgroundTint="#144A30"
                android:layout_margin="7dp"
                android:gravity="center"
                android:text="Approve"
                android:textColor="#F6F4F4">

            </TextView>

this is my fragment_one.xml file

<?xml version="1.0" encoding="utf-8"?>
<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"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context=".pending_req_admin">

    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbarStyle="outsideOverlay"
        tools:listitem="@layout/item_status_admin"
        android:scrollbars="vertical"
        android:id="@+id/pending_recyclerview">

    </androidx.recyclerview.widget.RecyclerView>

</LinearLayout>

this is the error

 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.work_home.pending_req_admin.onCreateView(pending_req_admin.java:51)
        at androidx.fragment.app.Fragment.performCreateView(Fragment.java:2963)
        at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:518)
        at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:282)
        at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
        at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
        at androidx.fragment.app.FragmentManager.execSingleAction(FragmentManager.java:1971)
        at androidx.fragment.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:311)
        at androidx.fragment.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:249)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1244)
        at androidx.viewpager.widget.ViewPager.populate(ViewPager.java:1092)
        at androidx.viewpager.widget.ViewPager.onMeasure(ViewPager.java:1622)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22002)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:958)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1514)
        at android.widget.LinearLayout.measureVertical(LinearLayout.java:806)
        at android.widget.LinearLayout.onMeasure(LinearLayout.java:685)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6580)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
        at com.android.internal.policy.DecorView.onMeasure(DecorView.java:721)
        at android.view.View.measure(View.java:22002)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2410)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1498)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1751)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1386)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6733)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:911)
        at android.view.Choreographer.doCallbacks(Choreographer.java:723)
        at android.view.Choreographer.doFrame(Choreographer.java:658)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:897)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
2021-08-07 08:24:08.185 28019-28019/com.example.work_home E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

So if you are familiar with this please consider this, Thank you

this is my java code

public class pending_req_admin extends Fragment {
    View v;
    RecyclerView pending_req_RecycleView;
    // ArrayList<Model> listModel;
    //RecyclerViewAdapterSt recyclerViewAdapterSt;
    FirebaseAuth auth = FirebaseAuth.getInstance();
    FirebaseUser FUser = auth.getCurrentUser();
    String UserId = FUser.getUid();
    FirebaseFirestore db = FirebaseFirestore.getInstance();
    FirestoreRecyclerAdapter adapter;
    TextView Btn_Approve_Status, Btn_Reject_Status;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment


        // Inflate the layout for this fragment
        v = inflater.inflate(R.layout.fragment_pending_req_admin, container, false);
        pending_req_RecycleView = v.findViewById(R.id.pending_recyclerview);
        Btn_Approve_Status = v.findViewById(R.id.btn_approve_status);
        Btn_Reject_Status = v.findViewById(R.id.btn_reject_status);

        //listModel = new ArrayList<>();
        // recyclerViewAdapterSt = new RecyclerViewAdapterSt(listModel);
        // statusRecycleView.setAdapter(recyclerViewAdapterSt);
        pending_req_RecycleView.setHasFixedSize(true);

        Query query = db.collection("users").document().collection("ApplyLeave");
        FirestoreRecyclerOptions<Model> options = new FirestoreRecyclerOptions.Builder<Model>()
                .setQuery(query, Model.class)
                .build();
        adapter = new FirestoreRecyclerAdapter<Model, pendingViewHolder>(options) {
            @NonNull
            @Override
            public pendingViewHolder onCreateViewHolder(@NonNull  ViewGroup parent, int viewType) {
                View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_status_admin,parent,false);
                return new pendingViewHolder(v);
            }

            @Override
            protected void onBindViewHolder(@NonNull  pending_req_admin.pendingViewHolder holder, int position, @NonNull  Model model) {
                holder.p_reason.setText(model.getU_reason());
                holder.p_name.setText(model.getU_nameL());
                holder.p_selectLeave.setText(model.getU_select_leave());
                holder.p_leaveType.setText(model.getU_leave_type());
                holder.p_fromDate.setText(model.getU_from_date());
                holder.p_toDate.setText(model.getU_to_date());


                Btn_Approve_Status.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getActivity(), "Leave Request Approved", Toast.LENGTH_SHORT).show();
                        Intent openSecondActivity = new Intent(pending_req_admin.this.getContext(), UserLeave.class);
                        startActivity(openSecondActivity);

                    }
                });

                Btn_Reject_Status.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(getActivity(), "Leave Request Rejected", Toast.LENGTH_SHORT).show();
                        Intent openSecondActivity = new Intent(pending_req_admin.this.getContext(), Home.class);
                        startActivity(openSecondActivity);

                    }
                });
            }
        };
        pending_req_RecycleView.setHasFixedSize(true);
        pending_req_RecycleView.setLayoutManager(new LinearLayoutManager(getActivity()));
        pending_req_RecycleView.setAdapter(adapter);

        return  v ;

    }

    private class pendingViewHolder extends RecyclerView.ViewHolder
    {
        TextView p_reason;
        TextView p_name;
        TextView p_selectLeave;
        TextView p_leaveType;
        TextView p_fromDate;
        TextView p_toDate;

        public pendingViewHolder(@NonNull  View itemView) {
            super(itemView);
            p_reason = itemView.findViewById(R.id.txt_reason_status);
            p_name = itemView.findViewById(R.id.txt_name_status);
            p_selectLeave = itemView.findViewById(R.id.txt_selectLeave_status);
            p_leaveType = itemView.findViewById(R.id.txt_leaveType_status);
            p_fromDate = itemView.findViewById(R.id.txt_fromDate_status);
            p_toDate = itemView.findViewById(R.id.txt_toDate_status);

        }
    }
    @Override
    public void onStart() {
        super.onStart();
        adapter.startListening();
    }

    @Override
    public void onStop() {
        super.onStop();
        adapter.stopListening();
    }
}

2

Answers


    1. Create an adapter class.

    2. Inflate your list_item_status_admin.xml layout into onCreateViewHolder().

                   @Override
                   public MovieListAdapter.Viewholder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
                       view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_status_admin, parent, false);
                       return new Viewholder(view);
                   }
      
    3. Assign your TextView id into ViewHolder() class in adapter.

                   public class Viewholder extends RecyclerView.ViewHolder {
                       RecyclerView pending_req_RecycleView;
      
                       public Viewholder(@NonNull @NotNull View itemView) {
                           super(itemView);
                           pending_req_RecycleView = itemView.findViewById(R.id.CV_movieInfo);
                       }
                   }
      
    4. Set Btn_Approve_Status setOnClickListener() in onBindViewHolder()

                   @Override
                   public void onBindViewHolder(@NonNull @NotNull MovieListAdapter.Viewholder holder, int position) {
                       holder.Btn_Approve_Status.setOnClickListener(new View.OnClickListener() {
                           @Override
                           public void onClick(View v) {
                               Toast.makeText(getActivity(), "Leave Request Approved", Toast.LENGTH_SHORT).show();
                               Intent openSecondActivity = new Intent(pending_req_admin.this.getContext(), UserLeave.class);
                               startActivity(openSecondActivity);
      
                           }
                       });
                   }
      
    5. Finally, add this adapter class in your recyclerView in pending_req_admin.java fragment.

                   pending_req_RecycleView.setLayoutManager(new LinearLayoutManager(getApplicationContext()));
                   pending_req_RecycleView.setAdapter(new YourAdaperclassName());
      

    Here is whole adapter class:

    public class adapter extends RecyclerView.Adapter<adapter.ViewHolder> {
                    Context mcontext;
                
                    public adapter(Context context) {
                        mcontext = context;
                    }
                
                    @NonNull
                    @NotNull
                    @Override
                    public adapter.ViewHolder onCreateViewHolder(@NonNull @NotNull ViewGroup parent, int viewType) {
                        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_status_admin, parent, false);
                        return new Viewholder(view);
                    }
                
                    @Override
                    public void onBindViewHolder(@NonNull @NotNull adapter.ViewHolder holder, int position) {
                        holder.Btn_Approve_Status.setOnClickListener(new View.OnClickListener() {
                            @Override
                            public void onClick(View v) {
                                Toast.makeText(mcontext, "Leave Request Approved", Toast.LENGTH_SHORT).show();
                                Intent openSecondActivity = new Intent(mcontext, UserLeave.class);
                                openSecondActivity.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                                mcontext.startActivity(openSecondActivity);
                            }
                        });
                    }
                
                    @Override
                    public int getItemCount() {
                        return 0;
                    }
                
                    public class ViewHolder extends RecyclerView.ViewHolder {
                        RecyclerView pending_req_RecycleView;
                
                        public ViewHolder(@NonNull @NotNull View itemView) {
                            super(itemView);
                            pending_req_RecycleView = itemView.findViewById(R.id.CV_movieInfo);
                        }
                    }
                }
    

    Note: you have to pass List in adapter constructor or just give static size in getItemCount() in adapter class.

    Login or Signup to reply.
  1. btn_reject_status is present in list_item_status_admin.xml and You are adding click listener of item layout views in Fragment.

    Need to create Custom Adapter and set that adapter to your recyclerview

    For reference check below Example code:

    Adapter Class:

    public class RecyclerMainAdapter extends FirestoreRecyclerAdapter<MainBeen, RecyclerMainAdapter.ViewHolder> {
    
       private RecyclerViewItemClick recyclerViewItemClick;
    
        public RecyclerMainAdapter(@NonNull FirestoreRecyclerOptions<MainBeen> options, RecyclerViewItemClick recyclerViewItemClick) {
            super(options);
            this.recyclerViewItemClick = recyclerViewItemClick;
        }
        @Override
        protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull MainBeen model) {
            holder.btnApproveStatus.setText(model.getITEM_DESCRIPTION());
            holder.btnApproveStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    recyclerViewItemClick.onTextViewClick(position);
                }
            });
        }
    
        @NonNull
        @Override
        public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item_status_admin,
                    parent, false);
            return new ViewHolder(v);
        }
        static class ViewHolder extends RecyclerView.ViewHolder {
    
            private TextView btnApproveStatus;
            public ViewHolder(View itemView) {
                super(itemView);
                btnApproveStatus = itemView.findViewById(R.id.btn_approve_status);
            }
        }
    
        
        /*Interface for communicating Adapter to Actvity/Fragment*/
        public  interface RecyclerViewItemClick{
            void onTextViewClick(int position);
        }
    }
    

    In Above code MainBeen is Model Class, You need to change this class according to your requirement.

    How to set adapter to RecyclerView in Fragment:

    RecyclerMainAdapter recyclerMainAdapter = new RecyclerMainAdapter(mainBeenOptions, new RecyclerMainAdapter.RecyclerViewItemClick() {
                @Override
                public void onTextViewClick(int position) {
                    // Handling click on recyclerview item textview
                    Toast.makeText(getActivity(), "Leave Request Approved", Toast.LENGTH_SHORT).show();
                    Intent openSecondActivity = new Intent(getContext(), UserLeave.class);
                    startActivity(openSecondActivity);
                }
            });
            pending_req_RecycleView.setLayoutManager(new LinearLayoutManager(getContent()));
            pending_req_RecycleView.setAdapter(recyclerMainAdapter);
    

    For more info related to RecyclerView Adapter check these links: Firestorerecycleradapter , RecyclerView.Adapter

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search