Mauker1 / MaterialSearchView

Android Search View based on Material design guidelines.
Other
1.08k stars 154 forks source link

error in adding Suggestions to search view #169

Closed ambitamber closed 4 years ago

ambitamber commented 4 years ago

This is my code. List<String> newWords = new ArrayList<>(); newWords.add("one"); newWords.add("two"); searchView.addSuggestions(newWords);

This is error that I am getting. java.lang.NullPointerException: Attempt to invoke virtual method 'void br.com.mauker.materialsearchview.MaterialSearchView.addSuggestions(java.util.List)' on a null object reference

Mauker1 commented 4 years ago

Did you place the MSV XML in your layout? Also, did you correctly get the reference using findViewById?

Can you share your layout and more of your code?

ambitamber commented 4 years ago

here is xml

    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    tools:context=".MainActivity">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="?attr/actionBarSize"
        android:background="#607D8B"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/bt_clearHistory"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear History"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <Button
        android:id="@+id/bt_clearSuggestions"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear Suggestions"
        app:layout_constraintTop_toBottomOf="@id/bt_clearHistory"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <Button
        android:id="@+id/bt_clearAll"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="clear All"
        app:layout_constraintTop_toBottomOf="@id/bt_clearSuggestions"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"/>

    <br.com.mauker.materialsearchview.MaterialSearchView
        android:id="@+id/searchView"
        style="@style/MaterialSearchViewStyle"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:elevation="10dp"
        app:voiceIconEnabled="true" />

</androidx.constraintlayout.widget.ConstraintLayout>

Here is Java file.

public class SearchActivty extends AppCompatActivity {

    @BindView(R.id.toolbar) Toolbar search_toolbar;
    @BindView(R.id.searchView) MaterialSearchView searchView;

    FireStoreLoader fireStoreLoader;
    String mQuery = null;
    GridLayoutManager gridLayoutManager;
    RecyclerViewAdapter recyclerViewAdapter;

    boolean dataAvailable = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);

        ButterKnife.bind(this);

        setSupportActionBar(search_toolbar);
        getSupportActionBar().setTitle(null);
        Handler handler = new Handler();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                searchView.openSearch();
                fireStoreLoader = new FireStoreLoader(SearchActivty.this);
                fireStoreLoader.getWordsList();
            }
        }, 300);
        searchViewText();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.search_menu,menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == R.id.action_search){
            searchView.openSearch();
        }
        return true;
    }

    private void searchViewText(){
        searchView.setOnQueryTextListener(new MaterialSearchView.OnQueryTextListener() {
            @Override
            public boolean onQueryTextSubmit(String query) {
                mQuery = query;
                Log.i("SearchActivty",mQuery);
                return true;
            }

            @Override
            public boolean onQueryTextChange(String newText) {
                return false;
            }
        });
    }

    @Override
    public void onBackPressed() {
        if (searchView.isOpen()){
            searchView.closeSearch();
        }else{
            super.onBackPressed();
        }
    }

  public void addSuggestions(List<String> words){
        List<String> newWords = new ArrayList<>();
        newWords.add("one");
        newWords.add("two");
        searchView.addSuggestions(newWords);
}
Mauker1 commented 4 years ago

When exactly are you calling public void addSuggestions(List<String> words)?

Also, I see you're using Butter Knife. Can you add a breakpoint after the ButterKnife.bind(this); and confirm if the searchView is not null?

ambitamber commented 4 years ago

Hello,

I am trying to add suggestions from

ambitamber commented 4 years ago

Here I call

handler.postDelayed(new Runnable() { @Override public void run() { searchView.openSearch(); fireStoreLoader = new FireStoreLoader(SearchActivty.this); fireStoreLoader.getWordsList(); } }, 300); searchViewText();

This is FireStoreLoader class. Here I use Firebase Firestore to fetch data.

`public void getWordsList(){ database = FirebaseFirestore.getInstance(); searchActivty = new SearchActivty(); String countryName = context.getResources().getConfiguration().locale.getCountry(); DocumentReference docRef = database.collection("Google Trend Data").document("india"); docRef.get().addOnSuccessListener(new OnSuccessListener() { @Override public void onSuccess(DocumentSnapshot documentSnapshot) { Object objectData = documentSnapshot.getData(); String jsonData = new Gson().toJson(objectData); try { JSONObject jsonObject = new JSONObject(jsonData); wordsList = new ArrayList<>(); for (int i = 0; i < jsonObject.length(); i++){ JSONObject eachItem = jsonObject.getJSONObject(String.valueOf(i)); String eachWord = eachItem.getString("word"); wordsList.add(eachWord); } } catch (JSONException e) { e.printStackTrace(); }

            searchActivty.addSuggestions(wordsList);
        }
    });

}`
ambitamber commented 4 years ago

I checked that search view is not null.

Mauker1 commented 4 years ago

Use your debugger again and check if MSV is null inside the public void addSuggestions(List<String> words) method.

ambitamber commented 4 years ago

That was the problem. Thanks for the help.

Mauker1 commented 4 years ago

You're welcome!