I have seen lots of question like this but can't seem to get any of the solutions to work for me in my situation. I am using android studio, If you need any more info or files let me know and I will post.
I will post my code here first
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());
}
});
}
});
}
}
Here is the Driver class
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;
}
}
The data will upload to the database on firebase but then the app will crash and I get the error
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.
When i click export JSON on what has been added to the database on firebase I get this
{
"lat" : "66",
"lon" : "85",
"name" : "mickjj"
}
Thanks