由于我不赞成应用兼容,因此我的搜索 View 不再起作用:

 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$1.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$1.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)

所以当我有searchview的nullpointer时:
@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);
    }

在我的菜单中有:
<!-- 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"/>

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

问候,

最佳答案

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

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

08-05 06:22