使用支持搜索小部件时,onCreateOptionsMenu()方法将引发空指针

时间:2018-09-23 01:41:23

标签: android widget searchview

活动

使用#import android.support.v7.widget.SearchView;

self.mapView.settings.consumesGesturesInView = false

search_menu xml

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

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

        return true;
    }

当我使用支持搜索视图时,它会抛出一个空指针

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:yourapp="http://schemas.android.com/tools">
    <item
        android:id="@+id/search"
        android:icon="@drawable/ic_search"
        android:title="search"
        app:showAsAction="collapseActionView|always"
        yourapp:actionViewClass="android.support.v7.widget.SearchView" />
</menu>

但是当我将xml更改为搜索小部件而不是支持小部件时,如下所示,并更改了活动导入

#import android.widget.SearchView;

一切正常

java.lang.NullPointerException
        at .MainActivity.onCreateOptionsMenu(MainActivity.java:42)
        at android.app.Activity.onCreatePanelMenu(Activity.java:2585)
        at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378)
        at android.support.v7.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94)
        at android.support.v7.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549)
        at android.support.v7.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589)
        at android.support.v7.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869)
        at android.support.v7.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:230)
        at android.os.Handler.handleCallback(Handler.java:808)
        at android.os.Handler.dispatchMessage(Handler.java:103)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:5333)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
        at dalvik.system.NativeStart.main(Native Method)

如果有帮助,则在此行显示空poitner

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:yourapp="http://schemas.android.com/tools">
    <item android:id="@+id/search"
        android:title="search"
        android:icon="@drawable/ic_search"
        app:showAsAction="collapseActionView|always"
        app:actionViewClass="android.widget.SearchView" />

我已经检查了可用的建议解决方案..但是它们都不起作用..现在已经弃用了某些魂器

0 个答案:

没有答案