Every time I click on a profile in my application, it causes this crash and error and I wanted to know why and try to find it.
The error:
java.lang.ClassCastException: java.lang.Long cannot be cast to java.util.List
at com.example.soulforge.fragments.Profile.lambda$loadBasicData$4$Profile(Profile.java:274)
at com.example.soulforge.fragments.-$$Lambda$Profile$KwoPqF2dchhRaWfrPFWRwwexTfQ.onEvent(Unknown Source:4)
at com.google.firebase.firestore.DocumentReference.lambda$addSnapshotListenerInternal$2(DocumentReference.java:504)
at com.google.firebase.firestore.DocumentReference$$Lambda$3.onEvent(Unknown Source:6)
at com.google.firebase.firestore.core.AsyncEventListener.lambda$onEvent$0(AsyncEventListener.java:42)
at com.google.firebase.firestore.core.AsyncEventListener$$Lambda$1.run(Unknown Source:6)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
The part of the profile that causes the issue
followersList = (List<Object>) value.get("followers");
followingList = (List<Object>) value.get("following");
and the whole profile.java
public class Profile extends Fragment {
boolean isMyProfile = true;
String uid;
String userUID;
FirestoreRecyclerAdapter<PostImageModel, PostImageHolder> adapter;
private TextView nameTv, toolbarNameTv, statusTv, followingCountTv, followersCountTv, postCountTv;
private CircleImageView profileImage;
private Button followBtn;
private RecyclerView recyclerView;
private LinearLayout countLayout;
private FirebaseUser user;
private ImageButton editProfileBtn;
List<Object> followersList, followingList, followingList_2;
boolean isFollowed;
DocumentReference userRef, myRef;
public Profile() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false);
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init(view);
myRef = FirebaseFirestore.getInstance().collection("Users")
.document(user.getUid());
if (IS_SEARCHED_USER) {
isMyProfile = false;
userUID = USER_ID;
loadData();
} else {
isMyProfile = true;
userUID = user.getUid();
}
if (isMyProfile) {
editProfileBtn.setVisibility(View.VISIBLE);
followBtn.setVisibility(View.GONE);
countLayout.setVisibility(View.VISIBLE);
} else {
editProfileBtn.setVisibility(View.GONE);
followBtn.setVisibility(View.VISIBLE);
countLayout.setVisibility(View.GONE);
}
userRef = FirebaseFirestore.getInstance().collection("Users")
.document(userUID);
loadBasicData();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new GridLayoutManager(getContext(), 3));
loadPostImages();
recyclerView.setAdapter(adapter);
clickListener();
}
private void loadData() {
myRef.addSnapshotListener(new EventListener<DocumentSnapshot>() {
@Override
public void onEvent(@Nullable DocumentSnapshot value, @Nullable FirebaseFirestoreException error) {
if (error != null) {
Log.e("Tag_b", error.getMessage());
return;
}
if (value == null || !value.exists()) {
return;
}
followingList_2 = (List<Object>) value.get("Following");
}
});
}
private void clickListener() {
followBtn.setOnClickListener(v -> {
if (isFollowed) {
followersList.remove(user.getUid());
followingList_2.remove(userUID);
final Map<String, Object> map_2 = new HashMap<>();
map_2.put("following", followingList_2);
Map<String, Object> map = new HashMap<>();
map.put("followers", followersList);
userRef.update(map).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
followBtn.setText("Follow");
myRef.update(map_2).addOnCompleteListener(task1 -> {
if (task1.isSuccessful()) {
Toast.makeText(getContext(), "UnFollowed", Toast.LENGTH_SHORT).show();
} else {
Log.e("Tag_3", task1.getException().getMessage());
}
});
} else {
Log.e("Tag", "" + task.getException().getMessage());
}
});
} else {
followersList.add(user.getUid());
followingList_2.add(userUID);
Map<String, Object> map_2 = new HashMap<>();
map_2.put("following", followingList_2);
Map<String, Object> map = new HashMap<>();
map.put("followers", followersList);
userRef.update(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
followBtn.setText("UnFollow");
myRef.update(map_2).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(getContext(), "Followed", Toast.LENGTH_SHORT).show();
} else {
Log.e("Tag", " " + task.getException().getMessage());
}
}
});
} else {
Log.e("Tag", "" + task.getException().getMessage());
}
}
});
}
});
editProfileBtn.setOnClickListener(v -> {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(requireContext(), Profile.this);
});
}
private void init(View view) {
Toolbar toolbar = view.findViewById(R.id.toolbar);
assert getActivity() != null;
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
nameTv = view.findViewById(R.id.nameTv);
toolbarNameTv = view.findViewById(R.id.toolbarNameTV);
statusTv = view.findViewById(R.id.statusTv);
followersCountTv = view.findViewById(R.id.followersCountTv);
followingCountTv = view.findViewById(R.id.followingCountTv);
postCountTv = view.findViewById(R.id.postCountTv);
profileImage = view.findViewById(R.id.profileImage);
followBtn = view.findViewById(R.id.followBtn);
recyclerView = view.findViewById(R.id.recyclerView);
countLayout = view.findViewById(R.id.countLayout);
editProfileBtn = view.findViewById(R.id.edit_profileImage);
FirebaseAuth auth = FirebaseAuth.getInstance();
user = auth.getCurrentUser();
}
private void loadBasicData() {
userRef.addSnapshotListener((value, error) -> {
if (error != null) {
Log.e("Tag_0", error.getMessage());
return;
}
assert value != null;
if (value.exists()) {
String name = value.getString("name");
String status = value.getString("status");
String profileURL = value.getString("profileImage");
nameTv.setText(name);
toolbarNameTv.setText(name);
statusTv.setText(status);
followersList = (List<Object>) value.get("followers");
followingList = (List<Object>) value.get("following");
followersCountTv.setText("" + followersList.size());
followingCountTv.setText("" + followingList.size());
try {
Glide.with(getContext().getApplicationContext())
.load(profileURL)
.placeholder(R.drawable.ic_person)
.timeout(6500)
.into(profileImage);
} catch (Exception e) {
e.printStackTrace();
}
if (followingList.contains(userUID)) {
followBtn.setText("UnFollow");
isFollowed = true;
} else {
isFollowed = false;
followBtn.setText("Follow");
}
}
});
postCountTv.setText("" + LIST_SIZE);
}
private void loadPostImages() {
DocumentReference reference = FirebaseFirestore.getInstance().collection("Users").document(userUID);
Query query = reference.collection("Post Images");
FirestoreRecyclerOptions<PostImageModel> options = new FirestoreRecyclerOptions.Builder<PostImageModel>()
.setQuery(query, PostImageModel.class)
.build();
adapter = new FirestoreRecyclerAdapter<PostImageModel, PostImageHolder>(options) {
@NonNull
@Override
public PostImageHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.profile_image_items, parent, false);
return new PostImageHolder(view);
}
@Override
protected void onBindViewHolder(@NonNull PostImageHolder holder, int position, @NonNull PostImageModel model) {
Glide.with(holder.itemView.getContext().getApplicationContext())
.load(model.getImageUrl())
.timeout(6500)
.into(holder.imageView);
}
};
}
@Override
public void onStart() {
super.onStart();
adapter.startListening();
}
@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
Uri uri = result.getUri();
uploadImage(uri);
}
}
private void uploadImage(Uri uri) {
StorageReference reference = FirebaseStorage.getInstance().getReference().child("Profile Images");
reference.putFile(uri)
.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
reference.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String imageURL = uri.toString();
UserProfileChangeRequest.Builder request = new UserProfileChangeRequest.Builder();
request.setPhotoUri(uri);
user.updateProfile(request.build());
Map<String, Object> map = new HashMap<>();
map.put("profileImage", imageURL);
map.put("date", FieldValue.serverTimestamp());
FirebaseFirestore.getInstance().collection("Users")
.document(user.getUid())
.update(map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
Toast.makeText(getContext(), "Updated Successfully", Toast.LENGTH_SHORT).show();
else
Toast.makeText(getContext(), "Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
} else {
Toast.makeText(getContext(), "Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
private static class PostImageHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
public PostImageHolder(@NonNull View itemView) {
super(itemView);
imageView = itemView.findViewById(R.id.imageView);
}
}
}
Why is this happening ,and why is it a Long
instead of being a list and causing all this issues?