gpt4 book ai didi

java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 类型错误

转载 作者:行者123 更新时间:2023-12-02 03:14:12 25 4
gpt4 key购买 nike

我有一个带有 Fragment 类的 Android 项目:

package com.example.android.dtuguide;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


public class NoticesFragment extends Fragment {

private RecyclerView mNoticesList;
private DatabaseReference mDatabase;


public NoticesFragment() {
// Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_notices, container, false);


mDatabase = FirebaseDatabase.getInstance().getReference().child("notice");
mNoticesList = (RecyclerView) view.findViewById(R.id.notices_list);
mNoticesList.setHasFixedSize(true);
mNoticesList.setLayoutManager(new LinearLayoutManager(getActivity()));

return view;
}


@Override
public void onStart() {
super.onStart();

FirebaseRecyclerAdapter<Notices,NoticeViewHolder> firebaseRecyclerAdapter
= new FirebaseRecyclerAdapter<Notices, NoticeViewHolder>(

Notices.class,
R.layout.list_view_row,
NoticeViewHolder.class,
mDatabase

) {
@Override
protected void populateViewHolder(NoticeViewHolder viewHolder, Notices model, int position) {
viewHolder.setRefertext(model.getRefer());
viewHolder.setDatetext(model.getDate());
}
};

mNoticesList.setAdapter(firebaseRecyclerAdapter);

}

public static class NoticeViewHolder extends RecyclerView.ViewHolder{

View mView;
public NoticeViewHolder(View itemView) {
super(itemView);

mView = itemView;
}
public void setRefertext(String refertext){
TextView referText = (TextView) mView.findViewById(R.id.refer_text);
referText.setText(refertext);
}

public void setDatetext (String datetext){
TextView dateText = (TextView) mView.findViewById(R.id.date_text);
dateText.setText(datetext);

}
}




}

和一个通知:

package com.example.android.dtuguide;
public class Notices {

private String refer , link ;
private String date,count;

public Notices (){

}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getLink() {
return link;
}

public void setLink(String link) {
this.link = link;
}

public String getRefer() {
return refer;
}

public void setRefer(String refer) {
this.refer = refer;
}
}

当我运行该应用程序时,它显示以下错误:

.example.android.dtuguide E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.dtuguide, PID: 8456
com.google.firebase.database.DatabaseException: Can't convert object of type java.lang.String to type com.example.android.dtuguide.Notices
at com.google.android.gms.internal.zzanp.zze(Unknown Source)
at com.google.android.gms.internal.zzanp.zzb(Unknown Source)
at com.google.android.gms.internal.zzanp.zza(Unknown Source)
at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
at com.firebase.ui.database.FirebaseRecyclerAdapter.parseSnapshot(FirebaseRecyclerAdapter.java:147)
at com.firebase.ui.database.FirebaseRecyclerAdapter.getItem(FirebaseRecyclerAdapter.java:136)
at com.firebase.ui.database.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:176)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5471)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5504)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4741)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4617)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1994)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1390)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1353)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:574)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3028)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2906)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1482)
at android.support.v7.widget.RecyclerView.access$400(RecyclerView.java:147)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:294)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:603)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

我尝试寻找一些修复方法,但没有任何效果。如果有人可以帮助我了解问题所在,并在可能的情况下发布修复程序,我将非常感激。

谢谢!

最佳答案

我得到了答案,为 DatabaseReference 创建另一个引用并将子项调用到该 Databasereference enter image description here

关于java - com.google.firebase.database.DatabaseException : Can't convert object of type java. lang.String 类型错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40552284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com