1

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

Main-Class 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;
    }

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

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)
ora
  • 79
  • 8

3 Answers3

1

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);
        }
    }
}
Alex Mamo
  • 91,677
  • 13
  • 105
  • 138
  • I need to insert this inside my ..onCreateOptionsMenu – ora Apr 23 '18 at 18:37
  • Exactly. Just do it. – Alex Mamo Apr 23 '18 at 18:38
  • now it doesnt crush but it doesnt show anything,I click on the icon search and nothing is happening – ora Apr 23 '18 at 18:41
  • Try to add a log statement to see if the method is called. If it is called, it means that the problem is inside your method. Try also to log `fragmentTag` to see if the corresponding TAG of your `JobsFragment` is ending with `0`. It should since is the first fragment that is added. – Alex Mamo Apr 23 '18 at 18:44
  • `:switcher:2131230925:0 switcher:2131230925:1` – ora Apr 23 '18 at 18:58
  • So the code that i provided you, is correct. In this case the prolem is most likely in your method. Have your tried to use use a log statement to see if is called? – Alex Mamo Apr 23 '18 at 19:08
  • the method is never called,& I am pretty sure that the method is written wrong anyway.I just copy pasted from the link aboe – ora Apr 23 '18 at 19:18
  • 1
    There is a flow. A copy and paste is not enought. You need to do this: find the fragment, create a new adapter for each new character is typed on the search view. If you consider using Firestore, I recomend you see this [post](https://stackoverflow.com/questions/49596610/is-it-possible-to-use-algolia-query-in-firestorerecycleroptions/49607796). The same principle also applies, even if you are not using Firestore and Algolia. – Alex Mamo Apr 23 '18 at 19:27
  • Is there everything alright? Have you solved the issue? – Alex Mamo Apr 25 '18 at 08:11
  • No I haven't Alex,I have started working on another fragment. – ora Apr 25 '18 at 10:43
  • Did my answer helped you in any way? – Alex Mamo Apr 25 '18 at 11:43
  • Yes,at least now it doesn't crash – ora Apr 26 '18 at 05:12
0

You get a null object reference because you're not initializing the recycle member, either with direct instantiation in your fragment or with injection (as annotated in the example you linked).

From the example you linked,

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

should go in your fragment class.

Otherwise you will have to initialize it by inflating.

William Burnham
  • 4,234
  • 1
  • 13
  • 28
  • My recycle in the fragment displays correctly when I put a string and click search the app crashes on this line `jobsFragment.jobView(querySearch);` and then it says a null object how do I fix it? – ora Apr 21 '18 at 18:30
  • Can you post the stacktrace as part of your question? – William Burnham Apr 21 '18 at 18:32
  • when you debug, is `jobsFragment` null? – William Burnham Apr 21 '18 at 18:41
  • `private JobsFragment jobsFragment=new JobsFragment();` this is how I call the fragment inside the class which holds this fragment – ora Apr 21 '18 at 18:44
  • I think to add this method https://stackoverflow.com/a/37562572/9635446 in my fragment and then call it inside my class but there are many things that I dont know like `item` Idk what does it refer to – ora Apr 21 '18 at 18:46
0

You are doing this:

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

The fragment displayed in the Pager is not the one you're holding onto in the Activity. The one you instantiated as

private JobsFragment jobsFragment=new JobsFragment();

Actually never got created (i.e. onCreate was never called on it, it's not attached to the activity).

As a direct fix, replace with

adapter.addFragment(jobsFragment, "Jobs");

But I would suggest to rethink the architecture of the app.

Dennis K
  • 1,769
  • 15
  • 26
  • Hey Dennis can you help me with my new question? – ora Apr 23 '18 at 18:58
  • What new question? – Dennis K Apr 23 '18 at 19:05
  • I'll look into it. But you may want to accept this one if it fixed your issue. – Dennis K Apr 23 '18 at 19:08
  • Dennis its not this one,I have more than 4h with Alex trying to solve it,I would have accepted it as in all my questions – ora Apr 23 '18 at 19:10
  • Well, regardless of other issues you may have with your code, you definitely need to fix this one. I'm pretty sure your NPR is caused by you accessing a "dead" fragment. – Dennis K Apr 23 '18 at 19:14
  • paste . ofcode .org `/i3jsYvpnWv4CpXuDF9bxxs` please take a look – ora Apr 23 '18 at 19:17
  • I'm not sure what you changed. But you're still instantiating two instances of JobsFragment. The one in the member `jobsFragment` is the dead one, i.e. it's `recycle` never gets initialized, because `onCreateView` is never called. – Dennis K Apr 23 '18 at 19:29