我正在尝试为我的应用实现搜索过滤器,但我不知道该怎么做,我看到了一个问题this one here,但这只会让我更加困惑,这就是我到目前为止所做的:
我在这样的主要活动中称片段类为
private JobsFragment jobsFragment=new JobsFragment();
主班活动
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
final SearchView searchView=(SearchView) menu.findItem(R.id.action_search).getActionView();
SearchManager searchManager = (SearchManager) getSystemService(SEARCH_SERVICE);
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
@Override
public boolean onQueryTextSubmit(String text) {
Query querySearch = fb.collection("jobs").whereEqualTo("title", text);
if (!text.trim().isEmpty()){
jobsFragment.jobView(querySearch);
}
return false;
}
@Override
public boolean onQueryTextChange(String newText) {
if (newText.trim().isEmpty()){
}
return false;
}
});
return true;
}
RecycleView在JobsFragment中:
public void jobView(Query query){
recycle.setItemAnimator(new DefaultItemAnimator());
FirestoreRecyclerOptions<JobsLists> options = new FirestoreRecyclerOptions.Builder<JobsLists>()
.setQuery(query, JobsLists.class)
.build();
adapter = new FirestoreRecyclerAdapter<JobsLists, JobsViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final JobsViewHolder holder, final int position, @NonNull final JobsLists jobsmodel) {
holder.setTitle(jobsmodel.getTitle());
holder.setDate(jobsmodel.getDate());
holder.setCo_Name(jobsmodel.getCo_Name());
holder.setSkills(jobsmodel.getSkills());
holder.setLocation(jobsmodel.getLocation());
holder.view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Context context = v.getContext();
Intent intent = new Intent(context, JobView.class);
intent.putExtra("title", jobsmodel.getTitle());
intent.putExtra("skills", jobsmodel.getSkills());
intent.putExtra("company", jobsmodel.getCo_Name());
intent.putExtra("description", jobsmodel.getDescription());
context.startActivity(intent);
}
});
}
@NonNull
@Override
public JobsViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custom_card, parent, false);
return new JobsViewHolder(view);
}
@Override
public int getItemViewType(int position) {
return position;
}
};
recycle.setAdapter(adapter);
}
全栈跟踪
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setItemAnimator
(android.support.v7.widget.RecyclerView$ItemAnimator)' on a null object reference
at com.example.dell.ora.JobsFragment.jobView(JobsFragment.java:57)
at com.example.dell.ora.Maino$2.onQueryTextSubmit(Maino.java:71)
at android.support.v7.widget.SearchView.onSubmitQuery(SearchView.java:1189)
at android.support.v7.widget.SearchView$7.onEditorAction(SearchView.java:1166)
at android.widget.TextView.onEditorAction(TextView.java:5207)
最佳答案
这不是从活动中调用片段类中存在的方法的正确方法。您需要这样使用FragmentManager
:
FragmentManager fragmentManager = getSupportFragmentManager();
List<Fragment> fragments = getSupportFragmentManager().getFragments();
for (Fragment fragment : fragments) {
String fragmentTag = fragment.getTag();
if (fragmentTag != null) {
String lastCharacter = fragmentTag.substring(fragmentTag.length() - 1);
if (lastCharacter.equals("0")) {
JobsFragment jobsFragment = (JobsFragment) fragmentManager.findFragmentByTag(fragmentTag);
jobsFragment.jobView(querySearch);
}
}
}
关于java - RecycleView中的空对象引用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49958847/