void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)

こ雲淡風輕ζ 提交于 2019-12-12 13:32:45

问题


I am doing SearchView on CustomAdapter where it will search Gate Name. After i try run the app, it suddenly crash and give me the error as stated above. I do not know which part did i even do wrong that cause this issue to appear everytime i try to run my app.

MainActivity.java

@Override
public boolean onCreateOptionsMenu(Menu menu) {

    getMenuInflater().inflate(R.menu.main_menu, menu);

    MenuItem searchItem = menu.findItem(R.id.item_search);
    SearchView searchView = (SearchView) searchItem.getActionView();

    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {
        @Override
        public boolean onQueryTextSubmit(String query) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String newText) {
            List<Gate> gateSearchList = new ArrayList<Gate>();

            for (Gate gate : gateList) {
                if (gate.getGateName().toLowerCase().contains(newText.toLowerCase())) {
                    gateSearchList.add(gate);
                }
            }

            adapter = new GateAdapter(MainActivity.this, gateSearchList);
            lv.setAdapter(adapter);
            return true;
        }
    });

    return super.onCreateOptionsMenu(menu);
}
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (item.getItemId() == R.id.action_logout) {
        logout();
    }
    return super.onOptionsItemSelected(item);
}

main_menu.xml

 <item
    android:id="@+id/item_search"
    android:icon="@drawable/ic_action_search"
    android:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"
    android:title="Search">

</item>
<item android:id="@+id/action_logout"
    android:title="Log Out"
    app:showAsAction="never"/>

This is the error message in logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setOnQueryTextListener(android.support.v7.widget.SearchView$OnQueryTextListener)' on a null object reference

It also indicate that the error also came from this line of code:

searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

I hope someone can be able to provide me the solution to solve my issue.


回答1:


Change your android:actionViewClass to app:actionViewClass, because you are using SearchView from support lib

<item
    android:id="@+id/item_search"
    android:icon="@drawable/ic_action_search"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="ifRoom|collapseActionView"
    android:title="Search">



回答2:


Posting this here for others who might face the similar issue.

Following up Jasmin John's Point I Have solved the issue with pro-guard rule by adding

-keep class android.support.v7.widget.SearchView { *; }



回答3:


I was facing the same issue my code was working properly when I was debugging it, but when I build an apk and run it crashes.

I used minifyEnabled false in build.gradle file and it worked properly

buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }



回答4:


create a one file in res/values/xml/searchable.xml like this

<?xml version="1.0" encoding="utf-8"?>
 <searchable xmlns:android="http://schemas.android.com/apk/res/android"
  android:hint="search hint"
  android:label="@string/app_name" />

and add this meta data tag in manifest in application tag file like this

        <meta-data android:name="android.app.searchable"
        android:resource="@xml/searchable" />

change your oncreate menu like this

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main_menu, menu);

    final MenuItem item = menu.findItem(R.id.R.id.item_search);
    final SearchView searchView = (SearchView) MenuItemCompat.getActionView(item);

    return true;

}


来源:https://stackoverflow.com/questions/45277654/void-android-support-v7-widget-searchview-setonquerytextlistenerandroid-support

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!