RecycleView中的空对象引用 [英] Null Object Reference in RecycleView

查看:138
本文介绍了RecycleView中的空对象引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用实施搜索过滤器,但我不知道该怎么做。我看到了一个问题这个在这里但它只会让我更加困惑。这就是我迄今为止所做的:
我打电话给像我这样的主要活动中的片段类

I am trying to implement a search filter for my app but I don't know how to do it.I saw a question this one here but it only made me more confused.This is what I have done this far: I call the Fragment Class in my main activity like this

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中:

The RecycleView is in 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);

    }

Full StackTrace

Full StackTrace

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 ,如下所示:

This is not the correct way in which you can call from an activity a method that exist in a fragment class. You need to use FragmentManager like this:

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);
        }
    }
}

这篇关于RecycleView中的空对象引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆