无法退回到键入。尝试将java对象上传到Firebase实时数据库

时间:2017-02-14 15:03:36

标签: android firebase firebase-realtime-database

我已经看到很多像这样的问题,但似乎无法让任何解决方案在我的情况下适合我。我正在使用android studio,如果您需要更多信息或文件让我知道,我会发布。

我会先在这里发布我的代码

package com.example.mick.drivers6;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.firebase.client.DataSnapshot;
import com.firebase.client.Firebase;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;

//https://drivers6-55085.firebaseio.com/

public class MainActivity extends AppCompatActivity {
    private EditText editTextName;
    private EditText editLon;
    private EditText editLat;
    private TextView textViewPersons;
    private Button buttonSave;
    int count=0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Firebase.setAndroidContext(this);

        buttonSave = (Button) findViewById(R.id.buttonSave);
        editTextName = (EditText) findViewById(R.id.editTextName);
        editLat = (EditText) findViewById(R.id.editLatitude);
        editLon = (EditText) findViewById(R.id.editLongitude);

        textViewPersons = (TextView) findViewById(R.id.textViewPersons);

        //Click Listener for button
        buttonSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //Creating firebase object
                Firebase ref = new Firebase(Config.FIREBASE_URL);

                //Getting values to store
                String name = editTextName.getText().toString().trim();
                String lon = editLon.getText().toString().trim();
                String lat = editLat.getText().toString().trim();

                //Creating Person object

                Driver d = new Driver();
                //Adding values
                d.setName(name);
                d.setLat(lat);
                d.setLon(lon);

                //Storing values to firebase
                ref.setValue(d);
//                ref.child("Counter").setValue(count);
                count++;

                ref.addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot snapshot) {
                        for(DataSnapshot postSnapshot : snapshot.getChildren())
                        {
                            Driver d = postSnapshot.getValue(Driver.class);
//                            int numFromDB = postSnapshot.getValue(int.class);
//                            String test = " Count is on "+numFromDB;
                            String details = "Name : "+d.getName()+"\nLatitude : "+d.getLat()+"\nLongitude : "+d.getLon();
                            textViewPersons.setText(details);
                        }
                    }

                    @Override
                    public void onCancelled(FirebaseError firebaseError) {
                        System.out.println("The read failed: " + firebaseError.getMessage());

                    }
                });
            }
        });



    }
}

这是Driver类

package com.example.mick.drivers6;

/**
 * Created by Mick on 13/02/2017.
 */

public class Driver {
    private String name,lat,lon;

    public Driver() {
    }

    public String getLat() {
        return lat;
    }

    public void setLat(String lat) {
        this.lat = lat;
    }

    public String getLon() {
        return lon;
    }

    public void setLon(String lon) {
        this.lon = lon;
    }

    public String getName() {
        return name;
    }

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

数据将上传到firebase上的数据库,但随后应用程序将崩溃,我收到错误

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.mick.drivers6, PID: 2936
                  com.firebase.client.FirebaseException: Failed to bounce to type
                      at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
                      at com.example.mick.drivers6.MainActivity$1$1.onDataChange(MainActivity.java:68)
                      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
                      at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
                      at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
                      at android.os.Handler.handleCallback(Handler.java:739)
                      at android.os.Handler.dispatchMessage(Handler.java:95)
                      at android.os.Looper.loop(Looper.java:135)
                      at android.app.ActivityThread.main(ActivityThread.java:5254)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at java.lang.reflect.Method.invoke(Method.java:372)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                   Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.example.mick.drivers6.Driver] from String value; no single-String constructor/factory method
                      at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator._createFromStringFallbacks(StdValueInstantiator.java:428)
                      at com.fasterxml.jackson.databind.deser.std.StdValueInstantiator.createFromString(StdValueInstantiator.java:299)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.deserializeFromString(BeanDeserializerBase.java:1056)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializer._deserializeOther(BeanDeserializer.java:136)
                      at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:123)
                      at com.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
                      at com.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
                      at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:183)
                      at com.example.mick.drivers6.MainActivity$1$1.onDataChange(MainActivity.java:68) 
                      at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45) 
                      at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45) 
                      at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38) 
                      at android.os.Handler.handleCallback(Handler.java:739) 
                      at android.os.Handler.dispatchMessage(Handler.java:95) 
                      at android.os.Looper.loop(Looper.java:135) 
                      at android.app.ActivityThread.main(ActivityThread.java:5254) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at java.lang.reflect.Method.invoke(Method.java:372) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
Application terminated.

当我点击在firebase上添加到数据库的内容时导出JSON我得到了这个

{
  "lat" : "66",
  "lon" : "85",
  "name" : "mickjj"
}

由于

2 个答案:

答案 0 :(得分:1)

您正在位置Driver存储ref的实例。然后,您正在向该位置添加一个侦听器,并期望找到Driver实例的子节点。而只是使用快照值:snapshot.getValue(Driver.class)

或者如果您打算拥有多个驱动程序,请使用唯一键添加它们:ref.push().setValue(d)。然后你的侦听逻辑扫描孩子将会工作。

如果这是新开发,您还应该考虑使用新的Firebase SDK而不是旧版SDK。参考Upgrade Guide

答案 1 :(得分:0)

您可能需要在Driver.class中添加一个默认的空构造函数:

public Driver() {

}

这将允许您在尝试在导致错误的行中从数据库中读取类。