'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListen



我的SearchView不工作。。。我试图在RecyclerView中搜索,但它出现了JavaExeption错误。。。我已经做了我能做的每一件事,但问题仍然存在。请引导我,我是一名学生。空对象引用上的"void android.widget.SearchView.setOnQueryTextListener(android.witget.SearchView$OnQueryText监听器(">

支持适配器代码

public class SupportAdapter extends RecyclerView.Adapter<SupportAdapter.viewHolder> implements Filterable {
private static final  String TAG = "SupportAdapter";
private Context mContext;
private List<SupportList> mData;
private List<SupportList> mDataFull;
public static class viewHolder extends RecyclerView.ViewHolder{
private TextView Category;
private TextView City;
private TextView Contact;
private TextView Description;
private TextView Organisation;
private TextView Phone;
private TextView State;
private LinearLayout container;
private viewHolder(View itemView) {
super(itemView);
container = itemView.findViewById(R.id.supportContainer);
Category = itemView.findViewById(R.id.category);
City = itemView.findViewById(R.id.city);
Contact = itemView.findViewById(R.id.contact);
Description = itemView.findViewById(R.id.description);
Organisation = itemView.findViewById(R.id.name_of_org);
Phone = itemView.findViewById(R.id.phone);
State = itemView.findViewById(R.id.state);
}
}
public SupportAdapter(Context mContext, List<SupportList> mData) {
this.mContext = mContext;
this.mData = mData;
mDataFull = new ArrayList<>(mData);
}
@NonNull
@Override
public viewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext())
.inflate(R.layout.search_item_list,parent,false);
return new viewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull SupportAdapter.viewHolder holder, int position) {
holder.container.setAnimation(AnimationUtils.loadAnimation(mContext, R.anim.fade_scale_animation));
holder.Category.setText(mData.get(position).getCategory());
holder.City.setText(mData.get(position).getCity());
holder.Contact.setText(mData.get(position).getContact());
holder.Description.setText(mData.get(position).getDescription());
holder.Organisation.setText(mData.get(position).getOrganisation());
holder.Phone.setText(mData.get(position).getPhone());
holder.State.setText(mData.get(position).getState());
}
@Override
public int getItemCount() {
return mData.size();
}

@Override
public Filter getFilter() {
return mDataFullFilter;
}
private Filter mDataFullFilter = new Filter() {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
List<SupportList> filteredList = new ArrayList<>();
if (constraint == null || constraint.length() == 0){
filteredList.addAll(mDataFull);
}else {
String filterPattern =constraint.toString().toLowerCase().trim();
for(SupportList item : mDataFull){
if(item.getCity().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
else if (item.getCity().toLowerCase().contains(filterPattern)){
filteredList.add(item);
}
}
}
FilterResults results = new FilterResults();
results.values = filteredList;
return results;
}
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
mData.clear();
mData.addAll((List) results.values);
notifyDataSetChanged();
}
};

搜索片段

private RecyclerView NewRecyclerView;
private SupportAdapter supportAdapter;
private List<SupportList> mData;
private SearchView searchItem;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
ViewModelProviders.of(this).get(SearchViewModel.class);
View root = inflater.inflate(R.layout.fragment_search, container, false);
searchItem = root.findViewById(R.id.searchItem);
NewRecyclerView = root.findViewById(R.id.searchList);
supportAdapter = new SupportAdapter (getContext(),mData);
NewRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
NewRecyclerView.setAdapter(supportAdapter);

return root;
}


@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mData = new ArrayList<>();
getSupport();
searchItem.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String query) {
supportAdapter.getFilter().filter(query);
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
supportAdapter.getFilter().filter(newText);
return false;
}
});
}

Logcat

2020-04-28 00:58:02.828 12382-12382/com.praso.indiacovid_19 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.praso.indiacovid_19, PID: 12382
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.SearchView.setOnQueryTextListener(android.widget.SearchView$OnQueryTextListener)' on a null object reference
at com.praso.indiacovid_19.ui.search.SearchFragment.onCreate(SearchFragment.java:64)
at androidx.fragment.app.Fragment.performCreate(Fragment.java:2414)
at androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManager.java:1418)
at androidx.fragment.app.FragmentManagerImpl.addAddedFragments(FragmentManager.java:2646)
at androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2416)
at androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2372)
at androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2273)
at androidx.fragment.app.FragmentManagerImpl$1.run(FragmentManager.java:733)
at android.os.Handler.handleCallback(Handler.java:883)
at android.os.Handler.dispatchMessage(Handler.java:100)
at android.os.Looper.loop(Looper.java:228)
at android.app.ActivityThread.main(ActivityThread.java:7782)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:981)

res/menue/menu_main.xml

<item
android:id="@+id/menu_item_search"
android:title="Search"
android:icon="@drawable/ic_baseline_search_24"
app:showAsAction="always"
app:actionViewClass="androidx.appcompat.widget.SearchView"
/>

只需确保将这一行放在手动内的代码中

app:actionViewClass="androidx.appcompat.widget.SearchView"

相关内容

  • 没有找到相关文章

最新更新