我已经将一些值存储在实时数据库的教师集合中。 但是,当我尝试使用搜索栏时,我的应用程序崩溃了。
setadapter_method
private void setAdapter(final String searchedstring) {
databaseReference.child("teacher").addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
fullnamelist.clear();
recyclerView.removeAllViews();
int counter=0;
for (DataSnapshot snapshot:dataSnapshot.getChildren()){
String uid =snapshot.getKey();
String full_name=snapshot.child("name").getValue(String.class);
if(full_name.toLowerCase().contains(searchedstring)){
fullnamelist.add(full_name);
counter++;
}
if(counter==10)
break;
}
searchAdapter = new searchadapter(getActivity(),fullnamelist);
recyclerView.setAdapter(searchAdapter);
}
searchadapter.java
public class searchadapter extends RecyclerView.Adapter<searchadapter.SearchViewHolder> {
Context context;
ArrayList<String> fullnamelist;
class SearchViewHolder extends RecyclerView.ViewHolder{
TextView full_name;
public SearchViewHolder(@NonNull View itemView) {
super(itemView);
full_name=(TextView)itemView.findViewById(R.id.full_name);
}
}
public searchadapter(Context context, ArrayList<String> fullnamelist) {
this.context = context;
this.fullnamelist = fullnamelist;
}
@NonNull
@Override
public searchadapter.SearchViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.search_list, parent ,false);
return new searchadapter.SearchViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull SearchViewHolder holder, int position) {
holder.full_name.setText(fullnamelist.get(position));
}
@Override
public int getItemCount() {
return fullnamelist.size();
}
}
在片段中实现搜索栏,以便为片段创建视图
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v= inflater.inflate(R.layout.fragment_groupformation,null);
mfirestore=FirebaseFirestore.getInstance();
databaseReference= FirebaseDatabase.getInstance().getReference();
recyclerView=(RecyclerView)v.findViewById(R.id.recyclerview);
ET1=(EditText)v.findViewById(R.id.ET1);
ET2=(EditText)v.findViewById(R.id.ET2);
ET3=(EditText)v.findViewById(R.id.ET3);
ET4=(EditText)v.findViewById(R.id.ET4);
ET5=(EditText)v.findViewById(R.id.ET5);
search_edittext=(EditText)v.findViewById(R.id.search_edittext);
submit=(Button)v.findViewById(R.id.submit);
fullnamelist=new ArrayList<>();
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager((new LinearLayoutManager(this.getActivity())));
recyclerView.addItemDecoration(new DividerItemDecoration(getActivity(),LinearLayoutManager.VERTICAL));
search_edittext.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if(!s.toString().isEmpty()){
setAdapter(s.toString());
}
}
});
return v;
}
Error_log找到了我的手机。 该错误最有可能来自setadapter方法。但我无法弄清楚。
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.String.toLowerCase()' on a null object reference
at com.kiit.projectmanager.groupformation_fragment$2.onDataChange(groupformation_fragment.java:108)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.1.0:183)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:742)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5603)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:774)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
答案 0 :(得分:0)
更改此:
String full_name = snapshot.child("name").getValue(String.class);
对此:
String full_name = snapshot.child("full_name").getValue(String.class);
根据数据库,您有一个名为full_name
的子属性,因此需要将数据库中的确切名称作为参数放入方法child()
答案 1 :(得分:0)
您使用的无效密钥(“名称”)会导致full_name
变为null,从而导致Null指针异常。
尝试一下
String full_name=snapshot.child("full_name").getValue(String.class);