SearchView是null android

时间:2015-01-06 18:39:12

标签: android searchview

您好我正在尝试像在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>

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

3 个答案:

答案 0 :(得分:29)

在菜单布局中:

<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"

答案 1 :(得分:0)

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

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

并且必须导入

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

请仔细查看导入的类是android.support.v7.widget.SearchView。当这两部分不相同时,可能会发生空指针异常。

答案 2 :(得分:-1)

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