I have a pretty weird problem. This code was working a few days ago however now it is crashing my application. I am trying to loop through a nodes children.
private void getInfo()
{
FirebaseDatabase.getInstance().getReference()
.child("ForumResponses").child(forumID).addListenerForSingleValueEvent(new ValueEventListener()
{
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot)
{
for(DataSnapshot snap : dataSnapshot.getChildren())
{
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError)
{
}
});
}
If i remove the below line of code the application does not crash. Why is this?
ForumResponses forumResponses = snap.getValue(ForumResponses.class);
Below is error log
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertInteger(com.google.firebase:firebase-database##16.1.0:351)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToPrimitive(com.google.firebase:firebase-database##16.1.0:272)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database##16.1.0:197)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(com.google.firebase:firebase-database##16.1.0:178)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access$100(com.google.firebase:firebase-database##16.1.0:47)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database##16.1.0:580)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(com.google.firebase:firebase-database##16.1.0:550)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database##16.1.0:420)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database##16.1.0:214)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database##16.1.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database##16.1.0:212)
at Activities.ViewForumResponseActivity$1.onDataChange(ViewForumResponseActivity.java:95)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database##16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database##16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database##16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
Below is Forum Response Class
public class ForumResponses
{
private String id;
private String response;
private String author;
private String authorID;
private String usersWhoLiked;
private int liked;
public ForumResponses()
{
/*
Default no arg constructor
*/
}
public ForumResponses(String id, String response, String author, String authorID, String usersWhoLiked, int liked)
{
this.id = id;
this.response = response;
this.author = author;
this.authorID = authorID;
this.usersWhoLiked = usersWhoLiked;
this.liked = liked;
}
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
public String getResponse()
{
return response;
}
public void setResponse(String response)
{
this.response = response;
}
public String getAuthor()
{
return author;
}
public void setAuthor(String author)
{
this.author = author;
}
public String getAuthorID()
{
return authorID;
}
public void setAuthorID(String authorID)
{
this.authorID = authorID;
}
public String getUsersWhoLiked()
{
return usersWhoLiked;
}
public void setUsersWhoLiked(String usersWhoLiked)
{
this.usersWhoLiked = usersWhoLiked;
}
public int getLiked()
{
return liked;
}
public void setLiked(int liked)
{
this.liked = liked;
}
}
Below is image of database
You are getting the following error:
com.google.firebase.database.DatabaseException: Failed to convert a value of type java.util.HashMap to int
Because the type of your liked field inside your ForumResponses class is int and in your database as the field is actual an object. To solve this, either convert the field in the database to int or change the type of the field in your ForumResponses class to a Map<String, Object>.
Related
App Get Crushed....
error not showing in android studio but while I run the app it get crush and marked the error in
ModelCategory model = dataSnapshot.getValue(ModelCategory.class);
All code is correct but while I the app it get crushed.... the model name is same as in firebase storage but get crush.....
private void loadCategories() {
categoryArrayList = new ArrayList<>();
DatabaseReference reference = FirebaseDatabase.getInstance().getReference("Categories");
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
categoryArrayList.clear();
for (DataSnapshot dataSnapshot : snapshot.getChildren()) {
ModelCategory model = dataSnapshot.getValue(ModelCategory.class);
categoryArrayList.add(model);
}
adapterCategory = new AdapterCategory(AdminDashboardActivity.this, categoryArrayList);
binding.categoriesRv.setAdapter(adapterCategory);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
Model Category Class: all code is correct but app get crush......
public class ModelCategory {
String id, category, uid;
long timestamp;
public ModelCategory(String id, String category, String uid, long timestamp) {
this.id = id;
this.category = category;
this.uid = uid;
this.timestamp = timestamp;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
this is the logcat error is between stars linge
at com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
at **com.mohmand.library.AdminDashboardActivity$3.onDataChange(AdminDashboardActivity.java:78)**
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
Please add an empty constructor.
Like this.
public class ModelCategory {
String id, category, uid;
long timestamp;
public ModelCategory(String id, String category, String uid, long timestamp) {
this.id = id;
this.category = category;
this.uid = uid;
this.timestamp = timestamp;
}
public ModelCategory() {
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getCategory() {
return category;
}
public void setCategory(String category) {
this.category = category;
}
public String getUid() {
return uid;
}
public void setUid(String uid) {
this.uid = uid;
}
public long getTimestamp() {
return timestamp;
}
public void setTimestamp(long timestamp) {
this.timestamp = timestamp;
}
}
I'm having a problem while trying to update to Firebase real time database.
I'm getting the error "com.google.firebase.database.DatabaseException: Serializing Arrays is not supported, please use Lists instead
I think this might mean I need to use another way to update the data since I'm using a recyclerView instead of a listView, but since now I have:
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
snapshot.getRef().child("title").setValue(postTitleEdit.getText());
snapshot.getRef().child("desc").setValue(postTextEdit.getText());
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
The way I'm using to add data to firebase is:
mDatabaseReference.addChildEventListener(new ChildEventListener() {
#Override
public void onChildAdded(#NonNull DataSnapshot snapshot, #Nullable String previousChildName) {
Blog blog = snapshot.getValue(Blog.class);
blogList.add(blog);
blogRecyclerAdapter = new BlogRecyclerAdapter(PostListActivity.this, blogList);
recyclerView.setAdapter(blogRecyclerAdapter);
blogRecyclerAdapter.notifyDataSetChanged();
}
Could someone help me update the data? I'm kinda stuck here.
Here is my Blog class:
public class Blog {
public String title;
public String desc;
public String image;
public String timestamp;
public String userid;
public Blog() {
}
public Blog(String title, String desc, String image, String timestamp, String userid, String milkQty, String note) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String title, String desc) {
this.title = title;
this.desc = desc;
this.image = image;
this.timestamp = timestamp;
this.userid = userid;
}
public Blog(String toString) {
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getTimestamp() {
return timestamp;
}
public void setTimestamp(String timestamp) {
this.timestamp = timestamp;
}
public String getUserid() {
return userid;
}
public void setUserid(String userid) {
this.userid = userid;
}
}
new update_button
public void update_button(){
databaseReference.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot snapshot) {
databaseReference.child("Mblog").child("title").setValue(postTitleEdit);
Toast.makeText(CowDet.this, "updated", Toast.LENGTH_LONG).show();
CowDet.this.finish();
}
new error: com.google.firebase.database.DatabaseException: Found conflicting getters for name: getText
I'm getting this error on the line databaseReference.child("Mblog").child("title").setValue(postTitleEdit);
The error message is pretty explicit. Your Blog class seems to contain an array (with []), which isn't supported by Firebase's serializer. You should instead use a List class for the same purpose.
If you're having a hard time making that work, edit your question to include the Blog class.
I'm trying to load an object from my database but the dataSnapshot is not returning anything. I have a structure like this:
This is my Event object:
public class Event implements Serializable {
private String eventName, eventDescription;
private Author author;
private Venue venue;
private ArrayList<Comment> comments;
private ArrayList<Like> likes;
public Event() {
}
public Event(#NonNull String eventName, String eventDescription, Author author, Venue venue) {
this.eventName = eventName;
this.eventDescription = eventDescription;
this.author = author;
this.venue = venue;
public String getEventName() {
return eventName;
}
public void setEventName(String eventName) {
this.eventName = eventName;
}
public Author getAuthor() {
return author;
}
public void setAuthor(Author author) {
this.author = author;
}
public Venue getVenue() {
return venue;
}
public void setVenue(Venue venue) {
this.venue = venue;
}
public String getEventDescription() {
return eventDescription;
}
public void setEventDescription(String eventDescription) {
this.eventDescription = eventDescription;
}
public List<Comment> getComments() {
return comments;
}
public void setComments(ArrayList<Comment> comments) {
this.comments = comments;
}
public List<Like> getLikes() {
return likes;
}
public void setLikes(ArrayList<Like> likes) {
this.likes = likes;
}
}
And this is my code:
Event event;
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference();
databaseReference.child("Event").child("0").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
event = dataSnapshot.getValue(Event.class);
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
The event object comes back as null even though I'm pretty sure the path is correct
While producing a list of my Entity I can't convert this to a String.
This is my Team code:
public class Teams {
#SerializedName("teamName")
#Expose
private String teamName;
#SerializedName("matches")
#Expose
private Object matches;
#SerializedName("players")
#Expose
private Object players;
#SerializedName("id")
#Expose
private Integer id;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public Object getMatches() {
return matches;
}
public void setMatches(Object matches) {
this.matches = matches;
}
public Object getPlayers() {
return players;
}
public void setPlayers(Object players) {
this.players = players;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
I'm using the following get:
public interface SoccerStatsApi {
String jsonUrl = "https://soccerstatsapp.azurewebsites.net/api/";
#GET("teams")
Call<List<Teams>> getTeams();`
And this is where I want to convert it to a complete string:
Call<List<Teams>> call = soccerStatsApi.getTeams();
call.enqueue(new Callback<List<Teams>>() {
#Override
public void onResponse(Call<List<Teams>> call, Response<List<Teams>> response) { }
Response().body().toString() only returns the initial string, not the actual object values in the List/response. I've checked everywhere, but so far no answer I can actually work with.
cast Response.body() to List.
for example:
'''List teams = (List)response.body() ;'''
hope you have configured retrofit to use Gson as default json parser
I am using this library: https://github.com/stfalcon-studio/ChatKit to add chat to my app, i am getting this error when i try to upload a message to the firestore db:
FATAL EXCEPTION: main
Process: neoncore.com.onepiece, PID: 3907
java.lang.RuntimeException: Found conflicting getters for name getUser on class neoncore.com.onepiece.beans.chat.Message
at com.google.android.gms.internal.zzevb$zza.<init>(Unknown Source)
at com.google.android.gms.internal.zzevb.zzg(Unknown Source)
at com.google.android.gms.internal.zzevb.zza(Unknown Source)
at com.google.android.gms.internal.zzevb.zzbw(Unknown Source)
at com.google.firebase.firestore.zzk.zzcd(Unknown Source)
at com.google.firebase.firestore.CollectionReference.add(Unknown Source)
at neoncore.com.onepiece.activities.ChatDetail$1.onSubmit(ChatDetail.java:57)
at com.stfalcon.chatkit.messages.MessageInput.onSubmit(MessageInput.java:145)
at com.stfalcon.chatkit.messages.MessageInput.onClick(MessageInput.java:108)
at android.view.View.performClick(View.java:5210)
at android.view.View$PerformClick.run(View.java:21328)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5551)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
This is my updated message class:
public class Message implements IMessage,
MessageContentType.Image, /*this is for default image messages implementation*/
MessageContentType /*and this one is for custom content type (in this case - voice message)*/ {
private String id;
private String text;
private Date createdAt;
private User user;
private Image image;
private Voice voice;
public Message(String id, User user, String text) {
this(id, user, text, new Date());
}
public Message(String id, User user, String text, Date createdAt) {
this.id = id;
this.text = text;
this.user = user;
this.createdAt = createdAt;
}
#Override
public String getId() {
return id;
}
#Override
public String getText() {
return text;
}
#Override
public Date getCreatedAt() {
return createdAt;
}
#Override
public User getUser() {
return this.user;
}
#Override
public String getImageUrl() {
return image == null ? null : image.url;
}
public Voice getVoice() {
return voice;
}
public String getStatus() {
return "Sent";
}
public void setText(String text) {
this.text = text;
}
public void setCreatedAt(Date createdAt) {
this.createdAt = createdAt;
}
public void setImage(Image image) {
this.image = image;
}
public void setVoice(Voice voice) {
this.voice = voice;
}
public static class Image {
private String url;
public Image(String url) {
this.url = url;
}
}
public static class Voice {
private String url;
private int duration;
public Voice(String url, int duration) {
this.url = url;
this.duration = duration;
}
public String getUrl() {
return url;
}
public int getDuration() {
return duration;
}
}
}
and also my user class:
[public class User implements IUser {
private String id, name, avatar;
public User(String id, String name, String avatar) {
this.id = id;
this.name = name;
this.avatar = avatar;
}
public User() {
}
#Override
public String getId() {
return id;
}
#Override
public String getName() {
return name;
}
#Override
public String getAvatar() {
return avatar;
}
}][1]
this is the point where the error occurs:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_detail);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FirebaseUser currentUser = FirebaseAuth.getInstance().getCurrentUser();
firebaseFirestore = FirebaseFirestore.getInstance();
final Author author = new Author(currentUser.getUid(),currentUser.getDisplayName(),currentUser.getPhotoUrl().toString());
messageMessagesListAdapter = new MessagesListAdapter<>(author.getId(),null);
messagesList = findViewById(R.id.messagesList);
inputView = findViewById(R.id.input);
inputView.setInputListener(new MessageInput.InputListener() {
#Override
public boolean onSubmit(CharSequence input) {
CollectionReference reference = firebaseFirestore.collection("Messages");
final Message message = new Message(author,input.toString(), "",null,new Date());
reference.add(message).addOnCompleteListener(new OnCompleteListener<DocumentReference>() {
#Override
public void onComplete(#NonNull Task<DocumentReference> task) {
messageMessagesListAdapter.addToStart(message,true);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.d("MessageUPloadFailed",e.getMessage());
}
});
return true;
}
});
messagesList.setAdapter(messageMessagesListAdapter);
}
The error occurs at the point where i try to add the message to the firestore database where the add method is called.
database image
You are getting this error:
java.lang.RuntimeException: Found conflicting getters for name getUser on class Message
Because in Message class you have a getUser() method but you don't have a user field at all. You have instead an author field and the getter for this field should be:
#Override
public Author getAuthor() { //NOT getUser()
return author;
}