gpt4 book ai didi

android - SearchView 为 null android

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:48:51 27 4
gpt4 key购买 nike

您好,我正在尝试实现一个 SearchView,就像我们在 Gmail 中所做的那样。

@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 对象为空

list :

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

可搜索的.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>

谁能帮助理解为什么这个对象会变空?谢谢!!!!

最佳答案

在菜单布局中:

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

关于android - SearchView 为 null android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27804996/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com