public class MainActivity extends AppCompatActivity {
Button button1;
EditText edittext1;
EditText edittext2;
RecyclerView listview1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button) findViewById(R.id.button1);
edittext1=(EditText) findViewById(R.id.edittext1);
edittext2=(EditText) findViewById(R.id.edittext2);
ListView listview = (ListView) findViewById(R.id.listview1) ;
final ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1,android.R.id.text1) ;
listview.setAdapter(adapter) ;
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
database.child("message").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
Board board = dataSnapshot.getValue(Board.class); // Get Board
adapter.add(board.gettle(); // add to adapter.
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
} // Receive events of child below message
});
}
public void click1button(View v){
DatabaseReference database = FirebaseDatabase.getInstance().getReference();
Board board=new Board(edittext2.getText().toString(),edittext1.getText().toString());
database.child("message").push().setValue(board);
}
package com.example.myapplication100;
class Board {
String title;
String content;
Board(){}
Board(String title, String content){
this.title=title;
this.content=content;
}
public Object gettitle() {return title;}
public String getContent(){return content;}
}
E/eglCodecCommon: GoldfishAddressSpaceHostMemoryAllocator: ioctl_ping failed for device_type=5, ret=-1
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication100, PID: 6155
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.myapplication100.Board
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(com.google.firebase:firebase-database@@19.3.0:435)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(com.google.firebase:firebase-database@@19.3.0:231)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(com.google.firebase:firebase-database@@19.3.0:79)
at com.google.firebase.database.DataSnapshot.getValue(com.google.firebase:firebase-database@@19.3.0:203)
at com.example.myapplication100.MainActivity$1.onChildAdded(MainActivity.java:74)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@19.3.0:79)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@19.3.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@19.3.0:55)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
I couldn't find it. Did I miss anything?"T" Activating the emulator forces the shutdown.
android-studio
Can't convert object of type java.lang.String
This error occurs because it does not match the data format you are loading.
on Google "android firebase Can't convert object of type java.lang.String" That's all you need to do.
© 2024 OneMinuteCode. All rights reserved.