Java 由于应用程序兼容,Searchview 不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27049294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 03:44:21  来源:igfitidea点击:

Searchview doesn't work since app compat

javaandroidsearchmenuandroid-appcompat

提问by Laurenswuyts

Since I implented app compat my searchview doesn't work anymore:

由于我实现了应用程序兼容,因此我的搜索视图不再起作用:

 Process: com.laurenswuyts.witpa, PID: 26666
    java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.SearchView.setSearchableInfo(android.app.SearchableInfo)' on a null object reference
            at com.laurenswuyts.witpa.Activities.Events.EventActivity.onCreateOptionsMenu(EventActivity.java:75)
            at android.app.Activity.onCreatePanelMenu(Activity.java:2820)
            at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:275)
            at android.support.v7.app.ActionBarActivity.superOnCreatePanelMenu(ActionBarActivity.java:276)
            at android.support.v7.app.ActionBarActivityDelegate.onCreatePanelMenu(ActionBarActivityDelegate.java:79)
            at android.support.v7.widget.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:49)
            at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:459)
            at android.support.v7.internal.app.ToolbarActionBar.run(ToolbarActionBar.java:69)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

So nullpointer for searchview while I have it:

所以当我拥有它时,搜索视图的空指针:

@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.event_main, menu);

        // Get the SearchView and set the searchable configuration
        // Associate searchable configuration with the SearchView
        SearchManager searchManager =
                (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        SearchView searchView =
                (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(
                searchManager.getSearchableInfo(getComponentName()));


        return super.onCreateOptionsMenu(menu);
    }

And in my menu I have this:

在我的菜单中,我有这个:

<!-- Search Widget -->
  <item android:id="@+id/action_search"
      android:title="@string/action_search"
      android:icon="@drawable/ic_action_search"
      app:showAsAction="always"
      android:actionViewClass="android.support.v7.widget.SearchView"/>

I have no idea why it doesn't work anymore but it happened since I started using app compat 21.

我不知道为什么它不再起作用,但是自从我开始使用 app compat 21 后就发生了。

Regards,

问候,

采纳答案by Simas

Try using the custom appnamespace for your actionViewClasstoo:

尝试app为您使用自定义命名空间actionViewClass

app:actionViewClass="android.support.v7.widget.SearchView"/>

回答by Parag Sarda

This could also happen if you have proguard enabled and it is striping aways SearchViewclass. You would need to modify proguard settings to keep the class.

如果您启用了 proguard 并且它正在剥离SearchView类,也会发生这种情况。您需要修改 proguard 设置以保留该类。

See thisquestion for more details.

有关更多详细信息,请参阅问题。

回答by Martin Pfeffer

after a while of playing "run & error" I found a solution.. Looks like the UI-element isn't the reason what causes the error. After setting the search to the QueryListener it is working well. Here is some code:

在玩了一段时间“运行和错误”后,我找到了一个解决方案..看起来 UI 元素不是导致错误的原因。将搜索设置为 QueryListener 后,它运行良好。这是一些代码:

Activitywhich contains the SearchBar:

包含 SearchBar 的活动

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

        SearchView searchView = (SearchView) menu.findItem(R.id.search).getActionView();
        searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String s) {
                Log.d(TAG, "onQueryTextSubmit ");
                return false;
            }

            @Override
            public boolean onQueryTextChange(String s) {
                Log.d(TAG, "onQueryTextChange ");
                return false;
            }
        });

        return true;
    }

searchable.xml

可搜索文件

<searchable xmlns:android="http://schemas.android.com/apk/res/android"
                                      android:label="@string/app_name"
                                      android:hint="@string/search_hint"/>

And the "SearchBar" in menu.xml

menu.xml 中的“SearchBar”

     <item
            android:id="@+id/search"
            android:title="@string/search_title"
            android:icon="@drawable/abc_ic_search_api_mtrl_alpha"
            app:showAsAction="collapseActionView|ifRoom"
            app:actionViewClass="android.support.v7.widget.SearchView"/>

...

And last, but not least, the manifest(but this should be clear)...

最后但并非最不重要的是,清单(但这应该很清楚)......

  <activity
        android:name=".activities.MainActivity"
        android:label="@string/title_activity_main">
        <meta-data
            android:name="android.app.searchable"
            android:resource="@xml/searchable"/>
    </activity>

more information -> Docs

更多信息 ->文档

回答by Florian Schneider

It′s an additional information to Simas answer. I found this in another answer (https://stackoverflow.com/a/33400808/4949671) and it was very important for solving my exception:

这是 Simas 回答的附加信息。我在另一个答案(https://stackoverflow.com/a/33400808/4949671)中发现了这一点,这对于解决我的异常非常重要:

Notice that, it is, app:actionViewClassNOTandroid:actionViewClass

请注意,它是,app:actionViewClass不是android:actionViewClass

回答by Darush

Add the following line to proguard-rules.profile located inside appfolder:

将以下行添加到位于app文件夹内的proguard-rules.pro文件:

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

-保持类android.support.v7.widget.SearchView { *; }

回答by Kasun Wanniarachchi

After you've clicked on a result, your app is expecting that an operation has not completed and is trying to go further into an Intent argument.

单击结果后,您的应用程序预计操作尚未完成并尝试进一步处理 Intent 参数。

SearchView.OnSuggestionListenerand return truewhich notifies your app that the click operation has completed.

SearchView.OnSuggestionListener并返回true通知您的应用单击操作已完成。

        searchView.setOnSuggestionListener(new SearchView.OnSuggestionListener() {

            @Override
            public boolean onSuggestionClick(int position) {
                return true;
            }

            @Override
            public boolean onSuggestionSelect(int position) {
                return false;
            }
        });

回答by Masum

If you use progurdthen you have to add the following line into proguard-rules.profile

如果您使用progurd,则必须将以下行添加到 proguard-rules.pro文件中

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

-保持公共类android.support.v7.widget.** { *; }

or

或者

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

-保持类android.support.v7.widget.SearchView { *; }

回答by Atiar Talukdar

If if you minify in your build types like that then you need to add a single line your 'proguard-rules.pro' file.

如果您像这样缩小构建类型,则需要在“proguard-rules.pro”文件中添加一行。

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

Add line your 'proguard-rules.pro' file.

添加行您的 'proguard-rules.pro' 文件。

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

回答by jeevraj jangid

Issue occurs if Proguard is enabled. can be fixed by adding this to proguard rules

如果启用 Proguard,则会出现问题。可以通过将其添加到 proguard 规则来修复

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

-保持类android.support.v7.widget.SearchView { *; }

回答by Beloo

I had pretty similar issue with androidx.appcompat.widget.SearchView, but different cause. The crash occurs on an action button click

我有与 非常相似的问题androidx.appcompat.widget.SearchView,但原因不同。崩溃发生在单击操作按钮时

 Process: hotspotshield.android.vpn, PID: 26089
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
    at androidx.appcompat.widget.SearchView.adjustDropDownSizeAndPosition(SearchView.java:1373)
    at androidx.appcompat.widget.SearchView.onLayoutChange(SearchView.java:380)
    at android.view.View.layout(View.java:20690)
    at android.view.ViewGroup.layout(ViewGroup.java:6194)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)

The cause is missing attribute android:popupBackgroundin the theme. Actually material AppCompat themes has the required background, but despite my theme extends from Theme.AppCompat.Light, it still crashes. The only solution i have found is to override the attribute:

原因是android:popupBackground主题中缺少属性。实际上材料 AppCompat 主题具有所需的背景,但尽管我的主题从 扩展Theme.AppCompat.Light,但它仍然崩溃。我找到的唯一解决方案是覆盖该属性:

<style name="AppTheme" parent="Theme.AppCompat.Light">
    <item name="android:popupBackground" tools:ignore="PrivateResource">@drawable/abc_popup_background_mtrl_mult</item>
</style>

It is the default drawable which is used in AppCompat. It is marked as private, but you may use it though. Or declare background drawable by yourself

它是 AppCompat 中使用的默认可绘制对象。它被标记为私有,但您可以使用它。或者自己声明背景可绘制