2016-07-27 108 views
2

我一直在嘗試瞭解Firebase,在我的第一個示例中存在一個我根本不理解的錯誤。如果有人能幫助我,我會很感激Firebase未知屬性異常

THIS IS MY MAINACTIVITY.JAVA

package com.example.betterlaylow.firebasetestinsertretrieve; 

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; 

import java.util.HashMap; 
import java.util.Map; 

public class MainActivity extends AppCompatActivity { 

private EditText editTextName; 
private EditText editTextAddress; 
private TextView textViewPersons; 
private Button buttonSave; 

@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); 
    editTextAddress = (EditText) findViewById(R.id.editTextAddress); 

    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 address = editTextAddress.getText().toString().trim(); 

      //Creating Person object 
      Person person = new Person(); 

      //Adding values 
      person.setName(name); 
      person.setAddress(address); 

      //Storing values to firebase 
      ref.child("Person").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 
         Person person = postSnapshot.getValue(Person.class); 

         //Adding it to a string 
         String string = "Name: "+person.getName()+"\nAddress: "+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()); 
       } 
      }); 

     } 
    }); 
} 

} 

THIS IS MY PERSON.JAVA

package com.example.betterlaylow.firebasetestinsertretrieve; 

/** 
* Created by betterlaylow on 7/26/16. 
*/ 

public class Person { 
//name and address string 
private String name; 
private String address; 

public Person() { 
    /*Blank default constructor essential for Firebase*/ 
} 
//Getters and setters 
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; 
} 
} 

THIS IS MY CONFIG.JAVA

package com.example.betterlaylow.firebasetestinsertretrieve; 

/** 
* Created by betterlaylow on 7/26/16. 
*/ 
public class Config { 
public static final String FIREBASE_URL = "https://simplifiedcoding.firebaseio.com/"; 


} 

AND THIS ID ANDROID MONITOR

07-26 21:07:06.917 6408-6408/com.example.betterlaylow.firebasetestinsertretrieve E/AndroidRuntime:致命例外:main 進程:com.example.betterlaylow.firebasetestinsertretrieve,PID:6408 com.firebase.client。 FirebaseException:無法彈出以在com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) 處鍵入 at com.example.betterlaylow.firebasetestinsertretrieve.MainActivity $ 1 $ 1.onDataChange(MainActivity.java:66) 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:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native COM:方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 所致。 fasterxml.jac kson.databind.exc.UnrecognizedPropertyException:無法識別的字段「msg」(com.example.betterlaylow.firebasetestinsertretrieve.Person類),未標記爲可忽略(2個已知屬性:,「地址」,「名稱」]) at [Source: [email protected];行:1,列:9](通過引用鏈:com.example.betterlaylow.firebasetestinsertretrieve.Person [「msg」]) 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.f asterxml.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.betterlaylow.firebasetestinsertretrieve。MainActivity $ 1 $ 1.onDataChange(MainActivity.java:66) 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:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method 。在voke(本機方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

回答

0

您已將您的聽衆編碼爲預期在位置「Person」下有多個Person實例。要做到這一點,你需要創建一個使用DatabaseReferencepush()方法創建的每個人獨特的鍵:

 //Storing values to firebase 
     ref.child("Person").push().setValue(person); 

另外,如果您只想在位置一個Person「人」改變監聽器是這樣的:

  public void onDataChange(DataSnapshot snapshot) { 
        Person person = snapshot.getValue(Person.class); 

        //Adding it to a string 
        String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n"; 

        //Displaying it on textview 
        textViewPersons.setText(string); 

      } 

此外,您正在使用的Firebase SDK不是當前版本。如果您剛剛開始使用Firebase,爲何不使用the latest

0

你錯過@IgnoreExtraProperties從你的類。 寫下你的POJO課程是這樣的:

@IgnoreExtraProperties 
public class Person { 

private String name; 
private String address; 

public Person() { 
    /*Blank default constructor essential for Firebase*/ 
} 
//Getters and setters 
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; 
} 
}