Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

error in adding Suggestions to search view #169

Closed
ambitamber opened this issue Sep 6, 2020 · 9 comments
Closed

error in adding Suggestions to search view #169

ambitamber opened this issue Sep 6, 2020 · 9 comments

Comments

@ambitamber
Copy link

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
Copy link
Owner

Mauker1 commented Sep 6, 2020

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
Copy link
Author

ambitamber commented Sep 6, 2020

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
Copy link
Owner

Mauker1 commented Sep 6, 2020

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
Copy link
Author

Hello,

I am trying to add suggestions from

@ambitamber
Copy link
Author

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
Copy link
Author

I checked that search view is not null.

@Mauker1
Copy link
Owner

Mauker1 commented Sep 6, 2020

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

@ambitamber
Copy link
Author

That was the problem. Thanks for the help.

@Mauker1
Copy link
Owner

Mauker1 commented Sep 7, 2020

You're welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants