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
Create an adapter class.
Inflate your
list_item_status_admin.xml
layout intoonCreateViewHolder()
.Assign your TextView id into
ViewHolder()
class in adapter.Set Btn_Approve_Status setOnClickListener() in
onBindViewHolder()
Finally, add this adapter class in your recyclerView in
pending_req_admin.java
fragment.Here is whole adapter class:
Note: you have to pass List in adapter constructor or just give static size in
getItemCount()
in adapter class.btn_reject_status
is present inlist_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:
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:
For more info related to RecyclerView Adapter check these links: Firestorerecycleradapter , RecyclerView.Adapter