致命异常:主要com.firebase.client.FirebaseException:无法解析com.firebase.client.Firebase.setValueInternal上的快照

时间:2017-04-27 20:57:06

标签: android firebase firebase-realtime-database

我一直在研究Android应用程序以进行本地化。数据库连接到firebase以实时跟踪用户。在实施注册表单时,我遇到了这个错误,我永远无法摆脱它。每当我尝试注册为学生或教师时,应用程序崩溃。我引用了very close,但它也没有帮助我。 这是我的代码

package com.example.appla.iattended;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.firebase.client.Firebase;


public class SignUpActivity extends AppCompatActivity {

    EditText email, password,id, confirmPassword;
    String str_email, str_password,str_id, str_confirmPassword;
    Boolean email_verified, password_verified, isTA, id_verified;
    Firebase ref = new Firebase("https://yattend-755ad.firebaseio.com/");

    Button signUp;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        Firebase.setAndroidContext(this);
        email = (EditText)  findViewById(R.id.txt_signup_email);
        password = (EditText)  findViewById(R.id.txt_signup_password);
        confirmPassword = (EditText)  findViewById(R.id.txt_signup_confirm_password);
        id = (EditText)  findViewById(R.id.txt_signup_id);
        email.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    email.setHint("");
                else
                    email.setHint("GUC email");
            }
        });
        password.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    password.setHint("");
                else
                    password.setHint("Password");
            }
        });
        confirmPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    confirmPassword.setHint("");
                else
                    confirmPassword.setHint("Confirm Password");
            }
        });
        id.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus)
                    id.setHint("");
                else
                    id.setHint("XX-XXXXX");
            }
        });
        signUp = (Button) findViewById(R.id.btn_login);
        signUp.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //generate strings
                str_email = email.getText().toString();
                str_password = password.getText().toString();
                str_confirmPassword = confirmPassword.getText().toString();
                str_id = id.getText().toString();

               //checking password
                if(str_password.equals(str_confirmPassword)){

                    password_verified = true;
                }else{
                    password_verified = false;
                    Toast.makeText(SignUpActivity.this,
                            "Passwords do not match, please try again.", Toast.LENGTH_LONG).show();
                }

                //checking email
                if(str_email.endsWith("@student.guc.edu.eg")){
                    email_verified = true;
                    isTA =false;
                }else if(str_email.endsWith("@guc.edu.eg")) {
                    email_verified = true;
                    isTA=true;

                }else{
                    email_verified = false;
                    Toast.makeText(SignUpActivity.this,
                        "Incorrect Email. Please use your GUC email.", Toast.LENGTH_LONG).show();
                }

                //checking id
                if(str_id.contains("-") && str_id.length() > 4){
                    id_verified =true;
                }else if (str_id.equals("") && isTA) {
                    id_verified = true;
                } else{
                        id_verified =false;
                        Toast.makeText(SignUpActivity.this,
                            "Please insert GUC ID or if TA, leave blank.", Toast.LENGTH_LONG).show();
                    }


                //To firebase
                if(email_verified && password_verified && id_verified){
                    //Insert
                    User user = new User(str_email,str_password,str_id,isTA);
                    Firebase newRef = ref.child("Users").push();
                    newRef.setValue(user);
                    if(isTA){
                        //Redirect to TA's page
                        Intent myIntent = new Intent(SignUpActivity.this, StartSessionActivity.class);
                        SignUpActivity.this.startActivity(myIntent);
                    }else{
                        //Redirect to students page
                        Intent myIntent = new Intent(SignUpActivity.this, StudentActivity.class);
                        SignUpActivity.this.startActivity(myIntent);
                    }
                }

            }
        });


    }
}

这是用户类:

package com.example.appla.iattended;


public class User {
     String email, password,id;
     Boolean isTa;

    public User(String email, String password, String id,Boolean isTa){
        this.email=email;
        this.password=password;
        this.id=id;
        this.isTa=isTa;
    }
}

这是我按下按钮寄存器时生成的日志

FATAL EXCEPTION: main
                                                                           com.firebase.client.FirebaseException: Failed to parse to snapshot
                                                                               at com.firebase.client.Firebase.setValueInternal(Firebase.java:347)
                                                                               at com.firebase.client.Firebase.setValue(Firebase.java:238)
                                                                               at com.example.appla.iattended.SignUpActivity$5.onClick(SignUpActivity.java:122)
                                                                               at android.view.View.performClick(View.java:4211)
                                                                               at android.view.View$PerformClick.run(View.java:17267)
                                                                               at android.os.Handler.handleCallback(Handler.java:615)
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92)
                                                                               at android.os.Looper.loop(Looper.java:137)
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4898)
                                                                               at java.lang.reflect.Method.invokeNative(Native Method)
                                                                               at java.lang.reflect.Method.invoke(Method.java:511)
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
                                                                               at dalvik.system.NativeStart.main(Native Method)
                                                                            Caused by: java.lang.IllegalArgumentException: No serializer found for class com.example.appla.iattended.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
                                                                               at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2615)
                                                                               at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542)
                                                                               at com.firebase.client.Firebase.setValueInternal(Firebase.java:336)
                                                                               at com.firebase.client.Firebase.setValue(Firebase.java:238) 
                                                                               at com.example.appla.iattended.SignUpActivity$5.onClick(SignUpActivity.java:122) 
                                                                               at android.view.View.performClick(View.java:4211) 
                                                                               at android.view.View$PerformClick.run(View.java:17267) 
                                                                               at android.os.Handler.handleCallback(Handler.java:615) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                               at android.os.Looper.loop(Looper.java:137) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4898) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 
                                                                            Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class com.example.appla.iattended.User and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) )
                                                                               at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.failForEmpty(UnknownSerializer.java:59)
                                                                               at com.fasterxml.jackson.databind.ser.impl.UnknownSerializer.serialize(UnknownSerializer.java:26)
                                                                               at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:118)
                                                                               at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2593)
                                                                               at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2542) 
                                                                               at com.firebase.client.Firebase.setValueInternal(Firebase.java:336) 
                                                                               at com.firebase.client.Firebase.setValue(Firebase.java:238) 
                                                                               at com.example.appla.iattended.SignUpActivity$5.onClick(SignUpActivity.java:122) 
                                                                               at android.view.View.performClick(View.java:4211) 
                                                                               at android.view.View$PerformClick.run(View.java:17267) 
                                                                               at android.os.Handler.handleCallback(Handler.java:615) 
                                                                               at android.os.Handler.dispatchMessage(Handler.java:92) 
                                                                               at android.os.Looper.loop(Looper.java:137) 
                                                                               at android.app.ActivityThread.main(ActivityThread.java:4898) 
                                                                               at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                               at java.lang.reflect.Method.invoke(Method.java:511) 
                                                                               at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006) 
                                                                               at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773) 
                                                                               at dalvik.system.NativeStart.main(Native Method) 

2 个答案:

答案 0 :(得分:0)

我对此不太确定,因为您的错误消息似乎表明您使用的是旧版Firebase。所以首先我会建议是否是这种情况,看看migration instructions

当前版本的Firebase shows an expected structure,用于读取和写入数据库的类,如下所示:

@IgnoreExtraProperties
public class User {

    public String username;
    public String email;

    public User() {
        // Default constructor required for calls to DataSnapshot.getValue(User.class)
    }

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

}

我不确定这是如何转换为旧版本的,但您可能希望尝试包含空构造函数并将字段公开。

答案 1 :(得分:0)

  

未发现任何属性... EMPTY_BEAN

您的字段是包私有的,您没有getter。

添加一些getter或将字段设为公共字段。

如果您打算使用getValue(User.class)

,还需要添加一个空构造函数