我正在开发Android应用并将Firebase集成到其中。连接成功并且写入Firebase不是问题,当应用程序尝试接收要在应用程序中显示的数据时,应用程序崩溃。如果有人可以提供帮助我在下面添加了我的代码。
Capture_Meetings.java
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;
public class Capture_Meetings extends AppCompatActivity {
private EditText editTextName;
private EditText editTextAddress;
private EditText editDateTime;
private TextView textViewPersons;
private Button buttonSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_capture__meetings);
Firebase.setAndroidContext(this);
buttonSave = (Button) findViewById(R.id.buttonSave);
editTextName = (EditText) findViewById(R.id.editTextName);
editTextAddress = (EditText) findViewById(R.id.editTextAddress);
editDateTime = (EditText) findViewById(R.id.editDateTime);
textViewPersons = (TextView) findViewById(R.id.textViewPersons);
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 address = editTextAddress.getText().toString().trim();
String DateTime = editDateTime.getText().toString().trim();
//Creating Person object
final MeetingUser person = new MeetingUser();
//Adding values
person.setName(name);
person.setAddress(address);
person.setDateTime(DateTime);
//Storing values to firebase
ref.child("Person").push().setValue(person);
//Value event listener for realtime data update
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot)
{
for (DataSnapshot postSnapshot : snapshot.getChildren()) {
//Getting the data from snapshot
MeetingUser person = postSnapshot.getValue(MeetingUser.class);
//Adding it to a string
String string = "\n Name of School: "+person.getName()+"\n Date and Time:"+ person.getDateTime()+"\nMeeting Notes: "+person.getAddress()+"\n\n";
//Displaying it on textview
textViewPersons.setText(string);
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
System.out.println("The read failed: " + firebaseError.getMessage());
}
});
}
});
}
}
MeetingUser.java
- 构造函数
public class MeetingUser {
//name and address string
private String Key;
private String name;
private String address;
private String DateTime;
public MeetingUser() {
/*Blank default constructor essential for Firebase*/
}
//Getters and setters
public String getKey ()
{
return Key;
}
public void setKey (String Key)
{
this.Key = Key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getDateTime()
{
return DateTime;
}
public void setDateTime (String DateTime)
{
this.DateTime = DateTime;
}
}
崩溃日志:
com.firebase.client.FirebaseException: Failed to bounce to type
at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185)
at com.example.pooveshin.saica_sgb.Capture_Meetings$1$1.onDataChange(Capture_Meetings.java:67)
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.exc.UnrecognizedPropertyException: Unrecognized field "-KVZjE7GFxVENXAnemZF" (class com.example.pooveshin.saica_sgb.MeetingUser), not marked as ignorable (4 known properties: , "name", "dateTime", "address", "key"])
at [Source: java.io.StringReader@20f5d5e1; line: 1, column: 26] (through reference chain: com.example.pooveshin.saica_sgb.MeetingUser["-KVZjE7GFxVENXAnemZF"])
at com.fasterxml.jackson.databind.DeserializationContext.reportUnknownProperty(DeserializationContext.java:555)
at com.fasterxml.jackson.databind.deser.std.StdDeserializer.handleUnknownProperty(StdDeserializer.java:708)
at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.handleUnknownProperty(BeanDeserializerBase.java:1160)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:315)
at com.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
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.pooveshin.saica_sgb.Capture_Meetings$1$1.onDataChange(Capture_Meetings.java:67)
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)
答案 0 :(得分:0)
看看这个:
public void setKey (String Key)
{
this.Key = Key;
}
当它被反序列化时杰克逊认为该字段是key
但是你把它写成Key
因此,它不能映射到对象的字段。
将 private String Key
更改为 private String key
,与DateTime
相同,如果可能,请使用小写字段。