RecycleView中的空对象引用

时间:2018-04-21 18:15:40

标签: java android firebase

我正在尝试为我的应用实施搜索过滤器,但我不知道该怎么做。我看到了一个问题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);

    }

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)

3 个答案:

答案 0 :(得分:1)

这不是您从活动中调用片段类中存在的方法的正确方法。你需要像这样使用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); } } }

{{1}}

答案 1 :(得分:0)

您得到一个空对象引用,因为您没有初始化recycle成员,无论是在片段中直接实例化还是使用注入(在您链接的示例中注释)。

从您关联的示例中

@BindView(R.id.yourViewId)
RecyclerView recycle;

应该进入你的片段类。

否则你必须通过膨胀来初始化它。

答案 2 :(得分:0)

你这样做:

adapter.addFragment(new JobsFragment(), "Jobs");

寻呼机中显示的片段不是您在“活动”中保留的片段。你实例化的那个

private JobsFragment jobsFragment=new JobsFragment();

实际上从未创建过(即onCreate从未被调用过,它没有附加到活动中)。

作为直接修复,请替换为

adapter.addFragment(jobsFragment, "Jobs");

但我建议重新考虑应用程序的架构。