2017-08-07 61 views
0

我正在構建一個從firebase中檢索數據的應用程序。要在活動中顯示 數據,並且我在轉到從Firebase獲取數據的活動 時遇到錯誤。從Firebase檢索數據:FirebaseException:無法彈出以鍵入?


以下是 「以人反對」 main類的

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; 
} 

Java代碼getter和setter ..

public class Main2Activity 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_main2); 
    buttonSave = (Button) findViewById(R.id.buttonSave); 
    buttonSave = (Button) findViewById(R.id.buttonSave); 
    editTextName = (EditText) findViewById(R.id.editTextName); 
    editTextAddress = (EditText) findViewById(R.id.editTextAddress); 

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

    Firebase.setAndroidContext(this); 

    //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.child("Person").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()); 
       } 
      }); 

     } 
    }); 

} 

這是我的火力截圖.. enter image description here

應用程序的Crashlog

E/AndroidRuntime: FATAL EXCEPTION: main 
       Process: com.example.dell.database, PID: 22852 
       com.firebase.client.FirebaseException: Failed to bounce to type 
        at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:185) 
        at com.example.dell.database.Main2Activity$1$1.onDataChange(Main2Activity.java:60) 
        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:5910) 
        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:1405) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200) 
       Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not instantiate value of type [simple type, class com.example.dell.database.Person] 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.dell.database.Main2Activity$1$1.onDataChange(Main2Activity.java:60)  
        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:5910)  
        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:1405)  
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1200)  

I/Process:發送信號。 PID:22852 SIG:9 應用程序已終止。

+0

請在您的文章中包含您的崩潰日誌。 – UmarZaii

+0

在您的onDataChange中,您已經在person對象中,必須使用getValue直接從snapShot讀取數據 –

回答

0

要顯示該名稱,並請使用如下代碼地址:

ref.child("Person").addValueEventListener(new ValueEventListener() { 
@Override 
public void onDataChange(DataSnapshot snapshot) { 
    String name = postSnapshot.child("name").getValue(String.class); 
    String address = postSnapshot.child("address").getValue(String.class); 
    String string = "Name: "+name+"\nAddress: "+address+"\n\n"; 

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

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

當你寫人到數據庫中,你把它寫成:

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

這寫一個人在數據庫中,也如控制檯的屏幕截圖所示。

然後,當您讀取數據,你這樣做:

ref.child("Person").addValueEventListener(new ValueEventListener() { 
    @Override 
    public void onDataChange(DataSnapshot snapshot) { 
     for (DataSnapshot postSnapshot : snapshot.getChildren()) { 
      Person person = postSnapshot.getValue(Person.class); 

      String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n"; 

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

所以你Person節點的孩子。由於您只寫了一個人到數據庫,因此不需要該循環,並且實際上會破壞代碼。要正確執行此操作,請移除該循環:

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

    String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n"; 

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

} 

順便說一下,您使用的是較舊版本的Firebase。我建議升級到更新的版本,即使只是因爲你更可能獲得幫助。要升級,看看這裏:https://firebase.google.com/support/guides/firebase-android