从 android studio Firebase 提取数据时出现以下错误
I got the following error while pulling data from android studio Firebase
我无法理解这个问题。试图从实时数据库或 code.firebasteki 中提取信息到屏幕时出现这样的错误,这是什么问题?
本书
public class Book {
private String isim;
private String soyad;
private String numara;
public Book() {
}
public Book(String isim, String soyad, String numara) {
this.isim = isim;
this.soyad = soyad;
this.numara = numara;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
public String getNumara() {
return numara;
}
public void setNumara(String numara) {
this.numara = numara;
}
主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView mrecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mrecyclerView=(RecyclerView) findViewById(R.id.recyclerview_books);
new FirebaseDatabaseHelper().readBooks(new FirebaseDatabaseHelper.DataStatus() {
@Override
public void DataIsLoaded(List<Book> books, List<String> keys) {
new RecyclerView_Config().setConfig(mrecyclerView,MainActivity.this,books,keys);
}
@Override
public void DataIsInserted() {
}
@Override
public void DataIsUpdated() {
}
@Override
public void DataIsDeleted() {
}
});
}
RecyclerView_Config
public class RecyclerView_Config {
private Context mContext;
private BookAdapter mbookAdapter;
public void setConfig(RecyclerView recyclerView,Context context,List<Book> books,List<String>
keys){
mContext=context;
mbookAdapter=new BookAdapter(books,keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(mbookAdapter);
}
class BookItemView extends RecyclerView.ViewHolder{
private TextView misim;
private TextView msoyad;
private TextView mnumara;
private String key;
public BookItemView(ViewGroup parent){
super(LayoutInflater.from(mContext).
inflate(R.layout.book_list_item,parent,false));
misim=(TextView) itemView.findViewById(R.id.isim_textView);
msoyad=(TextView) itemView.findViewById(R.id.soyad_textView);
mnumara=(TextView) itemView.findViewById(R.id.numara_textView);
}
public void bind(Book book,String key){
misim.setText(book.getIsim());
msoyad.setText(book.getSoyad());
mnumara.setText(book.getNumara());
this.key=key;
}
}
class BookAdapter extends RecyclerView.Adapter<BookItemView>{
private List<Book> mbookList;
private List<String> mkeys;
public BookAdapter(List<Book> mbookList, List<String> mkeys) {
this.mbookList = mbookList;
this.mkeys = mkeys;
}
@NonNull
@Override
public BookItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new BookItemView(parent);
}
@Override
public void onBindViewHolder(@NonNull BookItemView holder, int position) {
holder.bind(mbookList.get(position),mkeys.get(position));
}
@Override
public int getItemCount() {
return mbookList.size();
}
}
}
FirebaseDatabaseHelper
public class FirebaseDatabaseHelper {
private FirebaseDatabase mDatabase;
private DatabaseReference mReferenceBook;
private List<Book> books=new ArrayList<>();
public interface DataStatus{
void DataIsLoaded(List<Book> books,List<String> keys);
void DataIsInserted();
void DataIsUpdated();
void DataIsDeleted();
}
public FirebaseDatabaseHelper() {
mDatabase=FirebaseDatabase.getInstance();
mReferenceBook=mDatabase.getReference("Kullanicilar");
}
public void readBooks(final DataStatus dataStatus){
mReferenceBook.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
books.clear();
List<String> keys=new ArrayList<>();
for(DataSnapshot keyNode:snapshot.getChildren()){
keys.add(keyNode.getKey());
Book book=keyNode.getValue(Book.class);
books.add(book);
}
dataStatus.DataIsLoaded(books,keys);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
firebase
E/AndroidRuntime:致命异常:main
进程:com.example.phonebook,PID:14998
com.google.firebase.database.DatabaseException: 无法将类型 java.lang.Long 的值转换为字符串
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access100 美元(CustomClassMapper.java:48)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232)
在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80)
在 com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203)
在 com.example.phonebook.FirebaseDatabaseHelper$1.onDataChange(FirebaseDatabaseHelper.java:38)
在 com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75)
在 com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63)
在 com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55)
在 android.os.Handler.handleCallback(Handler.java:883)
在 android.os.Handler.dispatchMessage(Handler.java:100)
在 android.os.Looper.loop(Looper.java:214)
在 android.app.ActivityThread.main(ActivityThread.java:7356)
在 java.lang.reflect.Method.invoke(本机方法)
在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
名为 numara
的字段作为字符串存在于您的 Book
class 中,而在您的数据库中包含一个数字。这就是您收到该错误的原因。要解决这个问题,只需像这样更改 class 的声明:
public class Book {
private String isim;
private String soyad;
//
private long numara;
public Book() {
}
//
public Book(String isim, String soyad, long numara) {
this.isim = isim;
this.soyad = soyad;
this.numara = numara;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
//
public long getNumara() {
return numara;
}
//
public void setNumara(long numara) {
this.numara = numara;
}
}
我无法理解这个问题。试图从实时数据库或 code.firebasteki 中提取信息到屏幕时出现这样的错误,这是什么问题?
本书
public class Book {
private String isim;
private String soyad;
private String numara;
public Book() {
}
public Book(String isim, String soyad, String numara) {
this.isim = isim;
this.soyad = soyad;
this.numara = numara;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
public String getNumara() {
return numara;
}
public void setNumara(String numara) {
this.numara = numara;
}
主要活动
public class MainActivity extends AppCompatActivity {
private RecyclerView mrecyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mrecyclerView=(RecyclerView) findViewById(R.id.recyclerview_books);
new FirebaseDatabaseHelper().readBooks(new FirebaseDatabaseHelper.DataStatus() {
@Override
public void DataIsLoaded(List<Book> books, List<String> keys) {
new RecyclerView_Config().setConfig(mrecyclerView,MainActivity.this,books,keys);
}
@Override
public void DataIsInserted() {
}
@Override
public void DataIsUpdated() {
}
@Override
public void DataIsDeleted() {
}
});
}
RecyclerView_Config
public class RecyclerView_Config {
private Context mContext;
private BookAdapter mbookAdapter;
public void setConfig(RecyclerView recyclerView,Context context,List<Book> books,List<String>
keys){
mContext=context;
mbookAdapter=new BookAdapter(books,keys);
recyclerView.setLayoutManager(new LinearLayoutManager(context));
recyclerView.setAdapter(mbookAdapter);
}
class BookItemView extends RecyclerView.ViewHolder{
private TextView misim;
private TextView msoyad;
private TextView mnumara;
private String key;
public BookItemView(ViewGroup parent){
super(LayoutInflater.from(mContext).
inflate(R.layout.book_list_item,parent,false));
misim=(TextView) itemView.findViewById(R.id.isim_textView);
msoyad=(TextView) itemView.findViewById(R.id.soyad_textView);
mnumara=(TextView) itemView.findViewById(R.id.numara_textView);
}
public void bind(Book book,String key){
misim.setText(book.getIsim());
msoyad.setText(book.getSoyad());
mnumara.setText(book.getNumara());
this.key=key;
}
}
class BookAdapter extends RecyclerView.Adapter<BookItemView>{
private List<Book> mbookList;
private List<String> mkeys;
public BookAdapter(List<Book> mbookList, List<String> mkeys) {
this.mbookList = mbookList;
this.mkeys = mkeys;
}
@NonNull
@Override
public BookItemView onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new BookItemView(parent);
}
@Override
public void onBindViewHolder(@NonNull BookItemView holder, int position) {
holder.bind(mbookList.get(position),mkeys.get(position));
}
@Override
public int getItemCount() {
return mbookList.size();
}
}
}
FirebaseDatabaseHelper
public class FirebaseDatabaseHelper {
private FirebaseDatabase mDatabase;
private DatabaseReference mReferenceBook;
private List<Book> books=new ArrayList<>();
public interface DataStatus{
void DataIsLoaded(List<Book> books,List<String> keys);
void DataIsInserted();
void DataIsUpdated();
void DataIsDeleted();
}
public FirebaseDatabaseHelper() {
mDatabase=FirebaseDatabase.getInstance();
mReferenceBook=mDatabase.getReference("Kullanicilar");
}
public void readBooks(final DataStatus dataStatus){
mReferenceBook.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
books.clear();
List<String> keys=new ArrayList<>();
for(DataSnapshot keyNode:snapshot.getChildren()){
keys.add(keyNode.getKey());
Book book=keyNode.getValue(Book.class);
books.add(book);
}
dataStatus.DataIsLoaded(books,keys);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
}
});
}
firebase
E/AndroidRuntime:致命异常:main 进程:com.example.phonebook,PID:14998 com.google.firebase.database.DatabaseException: 无法将类型 java.lang.Long 的值转换为字符串 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertString(CustomClassMapper.java:426) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:217) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToType(CustomClassMapper.java:179) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.access100 美元(CustomClassMapper.java:48) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:593) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:563) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertBean(CustomClassMapper.java:433) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.deserializeToClass(CustomClassMapper.java:232) 在 com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:80) 在 com.google.firebase.database.DataSnapshot.getValue(DataSnapshot.java:203) 在 com.example.phonebook.FirebaseDatabaseHelper$1.onDataChange(FirebaseDatabaseHelper.java:38) 在 com.google.firebase.database.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) 在 com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) 在 com.google.firebase.database.core.view.EventRaiser$1.run(EventRaiser.java:55) 在 android.os.Handler.handleCallback(Handler.java:883) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java:214) 在 android.app.ActivityThread.main(ActivityThread.java:7356) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
名为 numara
的字段作为字符串存在于您的 Book
class 中,而在您的数据库中包含一个数字。这就是您收到该错误的原因。要解决这个问题,只需像这样更改 class 的声明:
public class Book {
private String isim;
private String soyad;
//
private long numara;
public Book() {
}
//
public Book(String isim, String soyad, long numara) {
this.isim = isim;
this.soyad = soyad;
this.numara = numara;
}
public String getIsim() {
return isim;
}
public void setIsim(String isim) {
this.isim = isim;
}
public String getSoyad() {
return soyad;
}
public void setSoyad(String soyad) {
this.soyad = soyad;
}
//
public long getNumara() {
return numara;
}
//
public void setNumara(long numara) {
this.numara = numara;
}
}