Firebase序列化问题[已更新] [英] Firebase Serialization Issues [UPDATED]
问题描述
昨晚我问过这个问题,但我认为我没有提供足够的信息,所以我会尝试更加彻底!
I asked this question last night but I don't think I offered enough information, so I am going to try to be a lot more thorough!
我是试图使其成为当用户注册我的应用程序时,他们的数据以及其他一些变量将保存在Firebase实时数据库中。当帐户尝试注册时,我会继续收到序列化错误。
I am trying to make it so that when a user registers for my app, their data along with some other variables will be saved in Firebase Realtime Database. I continue to get a serialization error when an account tries to register.
在com.google.firebase.database类上找不到要序列化的属性.obfuscated.zza $ 3
No properties to serialize found on class com.google.firebase.database.obfuscated.zza$3
我已经尝试了无数的东西来修复错误,但似乎没有任何东西真正修复它或取得任何进展修复它。这是我目前使用的注册方法。
And I have tried countless things to fix the error but nothing really seems to be fixing it or making any progress towards fixing it. This is the registration method I am using currently.
firebaseAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//start profile activity here
User users = new User(username, email, wins, losses, balance);
FirebaseDatabase.getInstance().getReference("Users")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid())
.setValue(users)
.addOnCompleteListener
(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(RegisterActivity.this, "Registration successful.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(RegisterActivity.this, HomePage.class ));
} else {
Toast.makeText(RegisterActivity.this, task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressDialog.hide();
}
}
});
} else {
Toast.makeText(RegisterActivity.this, "Registration not successful, please try again.", Toast.LENGTH_SHORT).show();
progressDialog.hide();
}
}
});
特别是在这段代码上调用错误
And specifically the error is being called on this part of the code
.setValue(users)
我确保我的所有变量都是公开的,并且我已经为所有内容添加了getter和setter(我不知道是否有必要)。
I have made sure all of my variables are public, and I have added getters and setters for everything as well (which I do not know if it is necessary).
My用户类看起来像这样
My User class looks like this
public User(String userName, String email, int wins, int losses, double balance) {
this.userName = userName;
this.email = email;
this.wins = wins;
this.losses = losses;
this.balance = balance;
}
我也启用了proguard,我添加了规则来保持我的用户类和我的RegisterActivity类。这些是我添加的规则
And I also have proguard enabled, and I have added rules to -keep my User class and my RegisterActivity class. These are the rules I added in
-keepattributes Signature
-keepclassmembers com.example.brent.fifty_fifty.** {
*;
}
-keep class com.example.brent.fifty_fifty.RegisterActivity
-keep class com.example.brent.fifty_fifty.User
-keep class * {
public private *;
}
我不确定我的规则是否正确,或者我是否有一些规则那里有不必要的规则,但这都是一个学习过程,所以如果出现任何问题或者您对如何解决错误有任何建议请告诉我!
I am not sure if I have my rules right, or if I have some unnecessary rules in there, but this is all a learning process so please let me know if anything looks wrong or you have a suggestion on how to fix the error!
对不起经常发布
我的User.class
My User.class
package com.example.brent.fifty_fifty;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
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;
public class User {
public String userName, email, specUserName;
public int wins, losses;
public double balance;
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("Users/"+uid);
public User() {
}
public User(String userName, String email, int wins, int losses, double balance) {
this.userName = userName;
this.email = email;
this.wins = wins;
this.losses = losses;
this.balance = balance;
}
public interface MyCallback{
void onSuccess(String userName);
}
public void getUserName(final MyCallback myCallback) {
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
specUserName = dataSnapshot.child("userName").getValue(String.class);
myCallback.onSuccess(specUserName);
}
@Override
public void onCancelled(DatabaseError databaseError) {
throw databaseError.toException();
}
});
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getWins() {
return wins;
}
public void setWins(int wins) {
this.wins = wins;
}
public int getLosses() {
return losses;
}
public void setLosses(int losses) {
this.losses = losses;
}
public double getBalance() {
return balance;
}
public void setBalance(double balance) {
this.balance = balance;
}
}
推荐答案
这种情况正在发生,因为您在用户
类中添加了余额
对象其类型为 BigDecimal
。与关于 Firebase实时数据库数据色彩的官方文档一样, BigDecimal
不是受支持的数据类型。要解决此问题,您应该将 balance
对象的te类型更改为支持的数据类型。我建议你把它改成 Double
。
This is happening because you have added in your User
class the balance
object which is of type BigDecimal
. As in the official documentation regarding Firebase realtime databse data tyes, the BigDecimal
is not a supported data type. To solve this, you should change te type of your balance
object to a supported data type. I recommend you to change it to a Double
.
这篇关于Firebase序列化问题[已更新]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!