Android:单击SearchView菜单图标时出现NullPointer异常

时间:2015-10-20 11:10:43

标签: android android-actionbar searchview

我在ActionBar中有SearchView菜单,所以当我点击搜索菜单图标时,我会得到以下信息:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable$ConstantState android.graphics.drawable.Drawable.getConstantState()' on a null object reference
at com.android.internal.widget.ActionBarView$ExpandedActionViewMenuPresenter.expandItemActionView(ActionBarView.java:1665)
at com.android.internal.view.menu.MenuBuilder.expandItemActionView(MenuBuilder.java:1266)
at com.android.internal.view.menu.MenuItemImpl.expandActionView(MenuItemImpl.java:625)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:909)
at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:894)
at android.widget.ActionMenuView.invokeItem(ActionMenuView.java:587)
at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:141)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
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:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Error logged: uncaught

我不明白为什么会出现此错误。有谁知道如何解决这个问题?

修改

searchable.xml:

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

活动onCreateOptionsMenu:

    getMenuInflater().inflate(R.menu.main_page_menu, menu);
    SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
    SearchView searchView = (SearchView) menu.findItem(R.id.ac_search_gdz).getActionView();
    searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
    searchView.setIconifiedByDefault(false);

菜单xml:

<item
    android:id="@+id/ac_search_gdz"
    android:icon="@drawable/ic_menu_search"
    android:orderInCategory="700"
    android:title="@string/gdz_text"
    app:actionViewClass="android.support.v7.widget.SearchView"
    app:showAsAction="collapseActionView|ifRoom"
    android:visible="false"/>

使用In片段时

public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) { 
    searchMenuItem = menu.findItem(R.id.ac_search_gdz);
    searchMenuItem.setVisible(true);
    searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem);
    super.onCreateOptionsMenu(menu, inflater);
}

在Manifest中:

    ...
    <meta-data
        android:name="android.app.default_searchable"
        android:value=".ClassPage" /
    ...
    <activity
        android:name=".ClassPage" >
        <intent-filter>
            <action android:name="android.intent.action.SEARCH" />
        </intent-filter>
     <meta-data android:name="android.app.searchable" android:resource="@xml/searchable"/>
    </activity> 

0 个答案:

没有答案