上传值时FireBase数据库异常

时间:2016-11-20 13:55:43

标签: java android firebase-realtime-database

在运行logcat中遇到这些错误: E / AndroidRuntime:致命异常:主要                   处理:com.example.shikh.regie,PID:19366                   com.google.firebase.database.DatabaseException:无法使用类com.example.shikh.regie.User解析节点                       在com.google.android.gms.internal.zzamm.zza(未知来源)                       在com.google.android.gms.internal.zzamm.zzbt(未知来源)                       在com.google.android.gms.internal.zzamp.zzbu(未知来源)                       在com.google.firebase.database.DatabaseReference.setValue(未知来源)                       在com.example.shikh.regie.MainActivity $ 1.onClick(MainActivity.java:40)

不知道问题是什么。为什么它无法解析节点

MainActivity.java

public class MainActivity extends AppCompatActivity {
private EditText name,email,phone;
private Button btn;
private DatabaseReference mDatabse;
private String name1,email1,phone1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mDatabse = FirebaseDatabase.getInstance().getReference().child("AppUsers");

    name = (EditText) findViewById(R.id.editText);
    email = (EditText) findViewById(R.id.editText2);
    phone = (EditText) findViewById(R.id.editText3);

    name1 = name.getText().toString();
    email1 = email.getText().toString();
    phone1 = phone.getText().toString();
    btn = (Button) findViewById(R.id.button);
         btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Map<String, User> users = new HashMap<String, User>();
            DatabaseReference ref=mDatabse.child("AppUsers");
            mDatabse.setValue(email1,new User(name1,email1,phone1));
            mDatabse.setValue(users);
        }
    });

}
}

User.java

package com.example.shikh.regie;


public class User {
String name,email,phone;

public User(){}

public User(String name, String email, String phone) {
    this.name = name;
    this.email = email;
    this.phone = phone;
}

public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getEmail() {
    return email;
}

public void setEmail(String email) {
    this.email = email;
}

public String getPhone() {
    return phone;
}

public void setPhone(String phone) {
    this.phone = phone;
}
 }

1 个答案:

答案 0 :(得分:1)

此代码不起作用:

mDatabse.setValue(email1,new User(name1,email1,phone1));

唯一[setValue()的匹配重载(https://firebase.google.com/docs/reference/android/com/google/firebase/database/DatabaseReference.html#setValue(java.lang.Object,java.lang.Object))是:

  

public Task<Void> setValue (Object value, Object priority)

     

将数据和优先级设置为给定值。

在这个重载中,第二个参数是一个优先级值,它必须是一个原始对象。

您似乎更有可能希望这样做:

mDatabse.child("email1").setValue(new User(name1,email1,phone1));

这会将新用户设置为引用下的节点email1