SearchView 为空 android

SearchView is null android

您好,我正在尝试像在 Gmail 中一样实现 SearchView。

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    if (!mNavigationDrawerFragment.isDrawerOpen()) {
        getMenuInflater().inflate(R.menu.main, menu);

        searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);
        searchView = (SearchView) menu.findItem(R.id.action_search).getActionView();
        searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));


        restoreActionBar();
        return true;
    }

    return super.onCreateOptionsMenu(menu);
}

searchView 对象为空

清单:

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.dgp"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="18"
        android:targetSdkVersion="21" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/logo"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <meta-data
            android:name="android.app.default_searchable"
            android:value=".GameSearchActivity" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".GameInfoActivity"
            android:label="@string/title_activity_game_info" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".SettingsActivity"
            android:label="@string/title_activity_settings" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity
            android:name=".GameSearchActivity"
            android:label="@string/title_activity_game_search" >
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>

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

</manifest>

Searchable.xml

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

菜单布局:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.dgp.MainActivity" >

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

<item
    android:id="@+id/action_settings"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"/></menu>

任何人都可以帮助理解为什么这个对象会变空吗? 谢谢!!!!

尝试

MenuItem searchItem = menu.findItem(R.id.action_search);
searchView = MenuItemCompat.getActionView(searchItem);

在菜单布局中:

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

使用这个:

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

因此您需要在菜单项标签中使用 app 而不是 android :

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

要支持搜索视图,您需要使用

app:actionViewClass="android.support.v7.widget.SearchView"----在菜单xml文件

并且必须导入

android.support.v7.widget.SearchView 在 java 文件中

请仔细看输入class必须是android.support.v7.widget.SearchView。当这两个部分不相同时,可能会发生空指针异常。