NullPointerException at FirebaseRecyclerAdapter.startListening() - android

I've got a problem with my app. I am trying to show all my user's list and their details (status, phone number), but I keep on getting the null pointer exception error on the onStart method. Here is my code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_customer_status);
...
}
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mUserDbase, Users.class)
.build();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.activity_customer_status, parent, false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder usersViewHolder, int i, #NonNull Users users) {
usersViewHolder.setName(users.getName());
usersViewHolder.setPhone(users.getPhone());
usersViewHolder.setStatus(users.getStatus());
}
};
mUserList.setAdapter(firebaseRecyclerAdapter);
}
#Override
protected void onStop() {
super.onStop();
firebaseRecyclerAdapter.stopListening();
}
// View Holder Class
public static class UsersViewHolder extends RecyclerView.ViewHolder{
View mView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName (String name){
TextView userNameView = (TextView) mView.findViewById(R.id.name_text);
userNameView.setText(name);
}
public void setPhone (String phone){
TextView userNameView = (TextView) mView.findViewById(R.id.phone_text);
userNameView.setText(phone);
}
public void setStatus (String status){
TextView userNameView = (TextView) mView.findViewById(R.id.status_text);
userNameView.setText(status);
}
}
}
I've followed this question: Error in FirebaseListAdapter.startListening(), which explains that the activity life cycle works as (onCreate>onStart>onResume), so assuming that (i'm a newb) I placed my on start code above my firebaseRecyclerAdapter. I've also followed this question as a guide but it didn't help me either. Please help

You are using the pointer firebaseRecyclerAdapter before initializing it.
firebaseRecyclerAdapter = new FirebaseRecyclerAdapte(){...};
firebaseRecyclerAdapter.startListening();
mUserList.setAdapter(firebaseRecyclerAdapter);

Related

How can I get the currently logged in user and prevent him from searching for himself?

The method searchForFriends retrieves users stored in firebase database based on their
usernames. However a logged in user can search for himself too.
I tried couple solutions but none worked.
I tried to get the currentuserid and transform it to string variable even though
the issue continued.
searchIcon.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
String searchTextInput = searchText.getText().toString();
searchForFriends(searchTextInput);
}
});
private void searchForFriends(String searchTextInput)
{
Toast.makeText(FindFriendsActivity.this, "Searching...", Toast.LENGTH_SHORT).show();
Query searchForPeople = usersRef.orderByChild("username")
.startAt(searchTextInput).endAt(searchTextInput+"\uf8ff");
FirebaseRecyclerOptions<FindFriends> options
= new FirebaseRecyclerOptions.Builder<FindFriends>()
.setQuery(searchForPeople,FindFriends.class).build();
FirebaseRecyclerAdapter<FindFriends,FindFriendsViewHolder> adapter
= new FirebaseRecyclerAdapter<FindFriends, FindFriendsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull FindFriendsViewHolder holder, int position, #NonNull FindFriends model)
{
holder.userFullName.setText(model.getFullname());
holder.userStatus.setText(model.getStatus());
Picasso.get().load(model.getProfileimage()).into(holder.userProfileImage);
}
#NonNull
#Override
public FindFriendsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType)
{
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.show_searched_users,parent,false);
FindFriendsViewHolder viewHolder = new FindFriendsViewHolder(view);
return viewHolder;
}
};
searchResults.setAdapter(adapter);
adapter.startListening();
}
public static class FindFriendsViewHolder extends RecyclerView.ViewHolder
{
TextView userFullName, userStatus;
CircleImageView userProfileImage;
public FindFriendsViewHolder(#NonNull View itemView)
{
super(itemView);
userFullName = itemView.findViewById(R.id.show_searched_users_full_name);
userStatus = itemView.findViewById(R.id.show_searched_users_status);
userProfileImage = itemView.findViewById(R.id.searched_user_profile_image);
}
}

The error is inn the firebaseRecyclerAdapter view adapter section

the error message is : incompatible types: FirestoreRecyclerOptions cannot be converted to FirebaseRecyclerOptions ----
and the code is as follows.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_agencies);
firebaseFirestore= FirebaseFirestore.getInstance();
//views definition
agencyFeed = (RecyclerView)findViewById(R.id.agencyRecyclerView);
//------Requirements for retrieving data and storing it to recycler view---
//Query ----- for retrieving data from firestore
Query query = firebaseFirestore.collection("Agencies");
// recyclerOptions
FirestoreRecyclerOptions<AgencyModel> options = new FirestoreRecyclerOptions.Builder<AgencyModel>()
.setQuery(query,AgencyModel.class)
.build();
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
#NonNull
#Override
public AgencyViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.agency_cards,parent,false);
return new AgencyViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull AgencyViewHolder holder, int position, #NonNull AgencyModel model) {
holder.name.setText(model.getName());
holder.location.setText(model.getLocation());
}
};
agencyFeed.hasFixedSize();
agencyFeed.setLayoutManager(new GridLayoutManager(this,2));
agencyFeed.setAdapter(adapter);
}
//viewholder class
public class AgencyViewHolder extends RecyclerView.ViewHolder {
private TextView name, location;
public AgencyViewHolder(#NonNull View itemView) {
super(itemView);
name= itemView.findViewById(R.id.agency_name); //name of agency
location= itemView.findViewById(R.id.locationTextView);
}
}
#Override
protected void onStop() {
super.onStop();
adapter.stopListening();
}
#Override
protected void onStart() {
super.onStart();
adapter.startListening();
}
}
You are using Firestore according to your query, therefore you have to use FirestoreRecyclerAdapter and not FirebaseRecyclerAdapter. Therefore change:
adapter = new FirebaseRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){
into this:
adapter = new FirestoreRecyclerAdapter<AgencyModel, AgencyViewHolder>(options){

Fragment OnclickListener RecycleView

Hello I am having a hard time to implement this,I havent found any example to do it.I have gone up to this point myself but I am stuck!Any help would be appreciated
public class JobsFragment extends Fragment{
private List<JobsLists> jobsLists;
private RecyclerView recycle;
private FirebaseFirestore fb;
private FirestoreRecyclerAdapter<JobsLists, JobsViewHolder> adapter;
private static final String TAG ="" ;
public JobsFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View jobsView = inflater.inflate(R.layout.fragment_jobs, container, false);
recycle = jobsView.findViewById(R.id.recycle);
recycle.setLayoutManager(new LinearLayoutManager(this.getActivity()));
fb = FirebaseFirestore.getInstance();
Query query =
recycle.setItemAnimator(new DefaultItemAnimator());
FirestoreRecyclerOptions<JobsLists> options = new FirestoreRecyclerOptions.Builder<JobsLists>()
.setQuery(query, JobsLists.class)
.build();
adapter = new FirestoreRecyclerAdapter<JobsLists, JobsViewHolder>(options) {
#Override
protected void onBindViewHolder(#NonNull JobsViewHolder holder, final int position,#NonNull JobsLists jobsmodel) {
jobsmodel = jobsLists.get(position);
holder.setTitle(jobsmodel.getTitle());
holder.cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
// Intent intent = new Intent(context, JobView.class);
// intent.putExtra("title", .get(position));
// intent.putExtra("description", .get(position));
// context.startActivity(intent);
Toast.makeText(context,"clicked="+ jobsmodel.getTitle(position)),Toast.LENGTH_SHORT).show();
}
});
}
#NonNull
#Override
public JobsViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_card, parent, false);
return new JobsViewHolder(view);
}
};
recycle.setAdapter(adapter);
return jobsView;
}
private class JobsViewHolder extends RecyclerView.ViewHolder {
private View view;
private CardView cv;
JobsViewHolder(final View itemView) {
super(itemView);
view = itemView;
cv = itemView.findViewById(R.id.job_post);
}
private TextView ti;
public void setTitle(String name) {
ti = view.findViewById(R.id.title);
ti.setText(name);
}
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
if (adapter != null) {
adapter.stopListening();
}
} }
And I cant really pass params to the fragment constructor as many examples suggest!
I have seen examples where they implement OnclickListener but I don't know why it doesnt work in my class.Please give me some directions ,I am trying to learn!
Replace Toast.makeText(context,"clicked="+jobsmodel.getTitle(position)),Toast.LENGTH_SHORT).show() with
Toast.makeText(context,"clicked="+jobsmodel.getTitle(),Toast.LENGTH_SHORT).show();
I always implement the onclickListener under the constructor of the holder class , so, try to implement this under JobsViewHolder(final View itemView)
cv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Context context = v.getContext();
Toast.makeText(itemView.getContext(),"clicked="+jobsmodel.getTitle(position)),Toast.LENGTH_SHORT).show();
}
});

Output not showing when the firebase ui upgrade to 3.2.2

When i upgrade my firebase-ui version to 3.2.2 the firebaserecycleradapter doesn't show any output the code of mine. at first when i got multidex error then i upgrade all library version to lastest version on dependency of app lvl gradle and after upgrade the problem is found the Firebaserecycleradapter method then i search it and write after run the app the list view are not displaying plz help me.
onStart():
#Override
protected void onStart() {
super.onStart();
FirebaseRecyclerOptions <Users> options = new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(mUserDatabase,Users.class)
.build();
FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {
#NonNull
#Override
public UsersViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.user_list_layout, parent, false);
return new UsersViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull UsersViewHolder holder, int position, #NonNull Users model) {
holder.setName(model.getName());
holder.setStatus(model.getStatus());
holder.setRIDNo(model.getRidno());
holder.setUserImage(model.getThumb_image(), getApplicationContext());
//to get key
final String user_id = getRef(position).getKey();
holder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent profileIntent = new Intent(UsersActivity.this, ProfileActivity.class);
profileIntent.putExtra("user_id", user_id);
startActivity(profileIntent);
}
});
}
};
mUsersList.setAdapter(firebaseRecyclerAdapter);
}
and the viewholder is:
//User list viewholder
public static class UsersViewHolder extends RecyclerView.ViewHolder {
static View mView;
private CircleImageView mUserView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public static void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public static void setStatus(String status){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(status);
}
public static void setRIDNo(String ridno){
TextView userRIDNoView = (TextView) mView.findViewById(R.id.user_single_ridno);
userRIDNoView.setText(ridno);
}
public static void setUserImage(String thumb_image, Context ctx){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
}
In order to make your code work, please consider using the following steps.
Remove the static from the declaration of your UsersViewHolder class. Should be only:
public class UsersViewHolder extends RecyclerView.ViewHolder {/* ... */}
Remove the static keyword also from the mView field and all your methods. Your holder class should look like this:
public class UsersViewHolder extends RecyclerView.ViewHolder {
private View mView;
private CircleImageView mUserView;
public UsersViewHolder(View itemView) {
super(itemView);
mView = itemView;
}
public void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setStatus(String status){
TextView userStatusView = (TextView) mView.findViewById(R.id.user_single_status);
userStatusView.setText(status);
}
public void setRIDNo(String ridno){
TextView userRIDNoView = (TextView) mView.findViewById(R.id.user_single_ridno);
userRIDNoView.setText(ridno);
}
public void setUserImage(String thumb_image, Context ctx){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.with(ctx).load(thumb_image).placeholder(R.drawable.default_avatar).into(userImageView);
}
}
Move all the code from the onStart() method inside onCreate() method except this line of code:
super.onStart();
Make your firebaseRecyclerAdapter varaible global:
private FirebaseRecyclerAdapter<Users, UsersViewHolder> firebaseRecyclerAdapter;
Remove FirebaseRecyclerAdapter<Blog, BlogViewHolder> from the onCreate() method.
Add the following lines of code in the onStart() and onStop() methods.
#Override
protected void onStart() {
super.onStart();
firebaseRecyclerAdapter.startListening();
}
#Override
protected void onStop() {
super.onStop();
if(firebaseRecyclerAdapter != null) {
firebaseRecyclerAdapter.stopListening();
}
}
If you are using authentication, please also don't forget to add this line of code inside onStart() method:
firebaseAuth.addAuthStateListener(authStateListener);

Can't find what's wrong in onBindViewHolder in FirebaseRecyclerAdapter Class

I'm trying to display an image and three textViews in recyclerView using Firebase but all i'm getting is a blank page. I'm thinking there's something wrong with the onBindViewHolder method, but i can't seem to find what.
onCreate
mFoodList=(RecyclerView)findViewById(R.id.foodList);
mFoodList.setHasFixedSize(true);
mFoodList.setLayoutManager(new LinearLayoutManager(this));
mDatabase= FirebaseDatabase.getInstance().getReference().child("Item");
mQuery=mDatabase.orderByKey();
mOptions=new FirebaseRecyclerOptions.Builder<Food>().setQuery(mQuery, Food.class).build();
mAuth=FirebaseAuth.getInstance();
mAuthListener=new FirebaseAuth.AuthStateListener() {
#Override
public void onAuthStateChanged(#NonNull FirebaseAuth firebaseAuth) {
if(firebaseAuth.getCurrentUser()==null){
Intent loginIntent=new Intent(MenuActivity.this,MainActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
}
}
};
onStart - This is where i'm having problems.
#Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
FBRA = new FirebaseRecyclerAdapter<Food, FoodViewHolder>(mOptions) {
#Override
protected void onBindViewHolder(#NonNull FoodViewHolder viewHolder, int position, #NonNull Food model) {
viewHolder.setName(model.getName());
viewHolder.setPrice(model.getPrice());
viewHolder.setDesc(model.getDesc());
viewHolder.setImage(getApplicationContext(),model.getImage());
}
#Override
public FoodViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.singlemenuitem, parent, false);
return new FoodViewHolder(view);
}
};
mFoodList.setAdapter(FBRA);
}
FoodViewHolder
public static class FoodViewHolder extends RecyclerView.ViewHolder{
View mView;
public FoodViewHolder(View itemView) {
super(itemView);
mView=itemView;
}
public void setName(String name){
TextView food_name=(TextView)mView.findViewById(R.id.foodName);
food_name.setText(name);
}
public void setPrice(String price){
TextView food_price=(TextView)mView.findViewById(R.id.foodPrice);
food_price.setText(price);
}
public void setDesc(String desc){
TextView food_desc=(TextView)mView.findViewById(R.id.foodDesc);
food_desc.setText(desc);
}
public void setImage(Context ctx, String image){
ImageView food_image=(ImageView)mView.findViewById(R.id.foodImage);
Picasso.with(ctx).load(image).into(food_image);
}
}
Unless you're using FirebaseRecyclerOptions.Builder#setLifecycleOwner(this), you'll have to manually call FirebaseRecyclerAdapter#[start/stop]Listening(). I would recommend reading the documentation.
As said by SUPERCILEX, you are using FirebaseRecyclerOption.Builder,
so you need to manually start and stop FirebaseRecyclerView listening.
So put Onstart()
mFoodList.startListening();
and OnStop()
mFoodList.stopListening();

Categories

Resources