com.google.firebase.database.DatabaseException:无法将java.lang.String类型的对象转换为ERROR类型

时间:2016-11-11 16:31:29

标签: java android firebase android-recyclerview adapter

我有一个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);

        }
    }




}

Notices

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)

我试过寻找一些修复但没有任何效果。我真的很感激,如果有人能帮我理解问题是什么,如果可能,可以发布修复。

谢谢!

3 个答案:

答案 0 :(得分:2)

我得到了这个的答案,为DatabaseReference创建了另一个引用,并将子进程调用到该数据库引用enter image description here

答案 1 :(得分:0)

可能你的错误在你的适配器中,在这里:

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

                Notices.class,
                R.layout.list_view_row,
                NoticeViewHolder.class,
                mDatabase <- you are passing a DatabaseReference

        )

您可能需要将mDatabase更改为与Notices

相关的内容

答案 2 :(得分:0)

经过一些深度故障排除后,这是Notices构造函数方法和JSON文件的问题,我从firebase接收到了,纠正了我的JSON文件,瞧!