Firebase数据未显示并且出现错误



当我搜索任何东西时,它什么也没做,但当我按下按钮时,我的应用程序崩溃并出错。我不知道怎么了。

主要活动

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:关闭VM2020-02-22 19:57:17.805 15653-15653/com.example.crimerecords E/AndroidRuntime:

致命异常:main流程:com.example.crimerecords,PID:15653java.lang.NullPointerException:试图读取null对象引用上的字段"android.view.Viewandroidx.recyclerview.widget.RyclerView$ViewHolder.itemView"位于androidx.recyclerview.widget.CyclerView$Adapter.createViewHolder(recyclerview.java:7079(位于androidx.recyclerview.widget.CyclerView$Recycler.tryGetViewHolderForPositionByDeadline(recyclerview.java:6235(位于androidx.recyclerview.widget.CyclerView$Recycler.getViewForPosition(recyclerview.java:6118(位于androidx.recyclerview.widget.CyclerView$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.CyclerView.dispatchLayoutStep2(recyclerview.java:4134(位于androidx.recyclerview.widget.CyclerView.dispatchLayout(recyclerview.java:3851(位于androidx.recyclerview.widget.CyclerView.consumerPendingUpdateOperations(recyclerview.java:1897(在androidx.recyclerview.widget.CyclerView$1.run(recyclerview.java:414(在android.view.Cchoreographer$CallbackRecord.run(Choreographer.java:949(在android.view.Cchoreographer.doCallbacks(Choreographer.java:761(位于android.view.Cchoreographer.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(ActivityThreads.java:6669(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:858(

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

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是要用于显示数据库中节点的布局。

关于使用FirebaseRecyclerAdapter的FirebaseUI文档中已经很好地涵盖了这一切,所以我建议花点时间阅读。

相关内容

  • 没有找到相关文章

最新更新