Firebase 数据未显示并出现错误 [重复]

Posted

技术标签:

【中文标题】Firebase 数据未显示并出现错误 [重复]【英文标题】:Firebase data is not getting displayed and getting error [duplicate] 【发布时间】:2020-06-06 17:52:42 【问题描述】:

当我搜索任何内容时,它什么也没做,但当我按下按钮时,我的应用程序崩溃并出现错误。不知道怎么回事。

主要活动

public class CriminalRecords extends Fragment 

    private static final String TAG = "CriminalRecords";


    private EditText msearchfield;
    private ImageButton msearchbtn;
    private FloatingActionButton mFab;

    private RecyclerView mResultList;

    private DatabaseReference mCriminalDatabase;



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

        msearchfield=view.findViewById(R.id.SearchCriminal);
        msearchbtn = view.findViewById(R.id.SearchButton);
        mFab = view.findViewById(R.id.AddCriminals);
        mResultList = view.findViewById(R.id.Criminal_result_list);
        mResultList.setHasFixedSize(true);
        mResultList.setLayoutManager(new LinearLayoutManager(getContext()));

        mCriminalDatabase = FirebaseDatabase.getInstance().getReference("Criminals");

        mFab.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 

                Intent intent =new Intent(getContext(), Add_Criminals.class);
                startActivity(intent);

            
        );

        msearchbtn.setOnClickListener(new View.OnClickListener() 
            @Override
            public void onClick(View v) 
                String searchText = msearchfield.getText().toString();
                firebaseSearch(searchText);

            
        );

        return view;
    

    private void firebaseSearch(String searchText) 
        Query firebaseSearchQuery = mCriminalDatabase.orderByChild("name").startAt(searchText).endAt(searchText + "\uf8ff");

        Log.d(TAG, "firebaseSearch: searching");

        FirebaseRecyclerOptions<Criminalupload>options=new FirebaseRecyclerOptions.Builder<Criminalupload>()
                .setQuery(firebaseSearchQuery,Criminalupload.class)
                .build();

        FirebaseRecyclerAdapter<Criminalupload, CriminalViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Criminalupload, CriminalViewHolder>(options)
        

            @Override
            protected void onBindViewHolder(@NonNull CriminalViewHolder holder, int position, @NonNull Criminalupload model) 
                Log.d(TAG, "onBindViewHolder: BindView Active");
                holder.setDetails(getActivity().getApplicationContext(),model.getName(),model.getImageid(),model.getAddress1(),model.getAddress2(),model.getAddress3(),model.getMobnum());
                Log.d(TAG, "onBindViewHolder: Bindview done");
            

            @NonNull
            @Override
            public CriminalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
                return null;
            
        ;

        firebaseRecyclerAdapter.startListening();

        Log.d(TAG, "firebaseSearch: setting adapter");
        mResultList.setAdapter(firebaseRecyclerAdapter);
        Log.d(TAG, "firebaseSearch: addapter set");

    

    //View Holder Class
    public static class CriminalViewHolder extends RecyclerView.ViewHolder

        View mView;
        public CriminalViewHolder(View itemView) 
            super(itemView);

            mView = itemView;
        

        public void setDetails(Context ctx,String CName, String Cimg, String Addres1, String Addres2, String Addres3, Long Mob)
            Log.d(TAG, "setDetails: setting details");
            TextView cname = (TextView)mView.findViewById(R.id.cProfileName);
            TextView caddress1 = (TextView)mView.findViewById(R.id.CriminalAddress);
            TextView caddress2 = (TextView)mView.findViewById(R.id.CriminalAddress2);
            TextView caddress3 = (TextView)mView.findViewById(R.id.CriminalAddress3);
            TextView cmobile = (TextView)mView.findViewById(R.id.CriminalMobile);
            CircleImageView cprof = (CircleImageView)mView.findViewById(R.id.cProfileImg);

            cname.setText(CName);
            caddress1.setText(Addres1);
            caddress2.setText(Addres2);
            caddress3.setText(Addres3);
            cmobile.setText(Math.toIntExact(Mob));
            Glide.with(ctx).load(Cimg).into(cprof);

        

    


错误

2020-02-22 19:57:17.804 15653-15653/com.example.crimerecords D/androidRuntime:关闭 VM 2020-02-22 19:57:17.805 15653-15653/com.example.crimerecords E/AndroidRuntime:

致命异常:主要 进程:com.example.crimerecords,PID:15653 java.lang.NullPointerException:尝试从空对象引用上的字段“android.view.View androidx.recyclerview.widget.RecyclerView$ViewHolder.itemView”读取 在 androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7079) 在 androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235) 在 androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118) 在 androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114) 在 androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303) 在 androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627) 在 androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587) 在 androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665) 在 androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134) 在 androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:3851) 在 androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1897) 在 androidx.recyclerview.widget.RecyclerView$1.run(RecyclerView.java:414) 在 android.view.Choreographer$CallbackRecord.run(Choreographer.java:949) 在 android.view.Choreographer.doCallbacks(Choreographer.java:761) 在 android.view.Choreographer.doFrame(Choreographer.java:693) 在 android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935) 在 android.os.Handler.handleCallback(Handler.java:873) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:193) 在 android.app.ActivityThread.main(ActivityThread.java:6669) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

2020-02-22 19:57:17.820 15653-15653/com.example.crimerecords I/Process:发送信号。 PID:15653 SIG:9

【问题讨论】:

请阅读Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers? - 总结是这不是解决志愿者的理想方式,并且可能会适得其反。请不要将此添加到您的问题中。 【参考方案1】:

您的onCreateViewHolder 方法返回null,同时它应该为数据库中的数据返回一个新的视图持有者。

典型的实现如下所示:

public CriminalViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
    View view = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.criminal, parent, false);

    return new CriminalViewHolder(view);

其中R.layout.criminal 是您要用于显示数据库中的节点的布局。

using the FirebaseRecyclerAdapter 上的 FirebaseUI 文档对此进行了很好的介绍,因此我建议您花一些时间阅读。

【讨论】:

以上是关于Firebase 数据未显示并出现错误 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

尝试从firebase数据库显示图像到tableview [重复]

Firebase CocoaPods 出现重复符号链接器错误

main.js:2未捕获的参考错误:未定义firebase [重复]

使用 firebase 未显示回收站视图 - kotlin

F盘显示设备硬件出现致命错误,导致请求失败要怎样办啊

CocoaPods 停止安装 Firebase/Analytics 并出现错误