My Application is crashed when I click on ItemView of RecyclerView, There is no error showing in code. The data are load in toast but not pass to the other class or what don't know, but when I click any Item the appliaction is crash..
This is my Main RecyclerView Class
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Admin extends AppCompatActivity {
Button addUser;
RecyclerView mainUserRecyclerView;
UserAdapter adapter;
FirebaseDatabase database;
ArrayList<UserInfo> userInfoArrayList = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
mainUserRecyclerView = findViewById(R.id.mainUserRecyclerView);
mainUserRecyclerView.setLayoutManager(new LinearLayoutManager(this));
addUser = findViewById(R.id.addUserBtn);
database= FirebaseDatabase.getInstance();
DatabaseReference reference = database.getReference().child("userInfo");
reference.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
for(DataSnapshot dataSnapshot : snapshot.getChildren()){
UserInfo userInfo = dataSnapshot.getValue(UserInfo.class);
userInfoArrayList.add(userInfo);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(Admin.this,"Failed to get data",Toast.LENGTH_SHORT).show();
}
});
adapter = new UserAdapter(Admin.this,userInfoArrayList);
mainUserRecyclerView.setAdapter(adapter);
}
public void setAddUser(View view){
Intent intent = new Intent(this,user_detail.class);
startActivity(intent);
}
}
This is my Adapter Class
package com.example.rent;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
public class UserAdapter extends RecyclerView.Adapter<UserAdapter.viewHolder> {
Context admin;
ArrayList<UserInfo> userInfoArrayList;
public UserAdapter(Admin admin, ArrayList<UserInfo> userInfoArrayList) {
this.admin = admin;
this.userInfoArrayList = userInfoArrayList;
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(admin).inflate(R.layout.user_list_item,parent,false);
return new viewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull UserAdapter.viewHolder holder, int position) {
UserInfo userInfo = userInfoArrayList.get(position);
holder.user_name.setText(userInfo.getShopName());
holder.itemView.setOnClickListener(v -> {
Intent intent = new Intent(admin,Display_User.class);
//Toast.makeText(admin, "" userInfo.getOneMonthRent(), Toast.LENGTH_SHORT).show();
intent.putExtra("Name",userInfo.getShopName());
intent.putExtra("Rent",userInfo.getOneMonthRent());
intent.putExtra("Complex",userInfo.getComplexName());
intent.putExtra("PaidRent",userInfo.getPaidAmount());
intent.putExtra("DMonth",userInfo.getDefaultMonth());
intent.putExtra("PendingRent",userInfo.getPendingAmount());
admin.startActivity(intent);
});
}
@Override
public int getItemCount() {
return userInfoArrayList.size();
}
public static class viewHolder extends RecyclerView.ViewHolder {
TextView user_name;
public viewHolder(@NonNull View itemView) {
super(itemView);
user_name = itemView.findViewById(R.id.tvUserName);
}
}
}
And The display User class where I have to fetch the data
package com.example.rent;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Display_User extends AppCompatActivity {
TextView tvName,tvRent,tvComplex,tvPaidRent,tvDefaultMonth,tvPendingRent;
String Sname, rent, complex, paidRent, defaultMonth, pendingRent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.display_user);
Sname = getIntent().getStringExtra("Name");
rent = getIntent().getStringExtra("Rent");
complex = getIntent().getStringExtra("Complex");
paidRent = getIntent().getStringExtra("PaidRent");
defaultMonth = getIntent().getStringExtra("DMonth");
pendingRent = getIntent().getStringExtra("Pending");
tvName = findViewById(R.id.tvName);
tvName.setText(Sname);
tvRent = findViewById(R.id.tvRent);
tvRent.setText(rent);
tvComplex = findViewById(R.id.tvComplex);
tvComplex.setText(complex);
tvPaidRent = findViewById(R.id.tvPaidRent);
tvPaidRent.setText(paidRent);
tvDefaultMonth = findViewById(R.id.tvDefault);
tvDefaultMonth.setText(defaultMonth);
tvPendingRent = findViewById(R.id.tvPending);
tvPendingRent.setText(pendingRent);
}
}
CodePudding user response:
Have you declared the Activity on the manifest file? And the intent ?