gpt4 book ai didi

java - RecycleView 中的空对象引用

转载 作者:行者123 更新时间:2023-11-30 00:00:53 27 4
gpt4 key购买 nike

我正在尝试为我的应用实现搜索过滤器,但我不知道该怎么做。我看到了一个问题 this one here但这只会让我更加困惑。这就是我到目前为止所做的:我在我的主要 Activity 中这样调用 fragment 类

private JobsFragment jobsFragment=new JobsFragment();

主类 Activity

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)

最佳答案

这不是从 Activity 调用 fragment 类中存在的方法的正确方法。您需要像这样使用 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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com