我正在尝试创建一个搜索栏,然后我按照http://developer.android.com/training/search/setup.html上的教程进行操作。它不起作用,并且不会调用SearchResultsActivity,所以我立即找到了以下问题:
Search widget on action bar doesn't trigger my search activity。但是,那里列出的解决方案(更具体地说,在AndroidManifest.xml中添加元数据并使用xml / searchable.xml中的字符串引用)并不能解决我的问题。我也尝试将元数据放在AndroidManifest.xml中的<activity>
中,而其他许多解决方案都无济于事。
我做错了什么?
这是我的代码:
main.xml中:
<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.jsondataparser.app.MainActivity" >
<item android:id="@+id/action_settings"
android:title="@string/action_settings"
android:orderInCategory="100"
app:showAsAction="never" />
<item android:id="@+id/search_menu"
android:title="@string/app_name"
android:icon="@drawable/ic_launcher"
android:showAsAction="collapseActionView|ifRoom"
android:actionViewClass="android.widget.SearchView" />
</menu>
XML / 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/app_name">
</searchable>
的AndroidManifest.xml:
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity" />
<activity
android:name="com.jsondataparser.app.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="com.jsondataparser.app.SearchResultsActivity">
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
MainActivity.java:
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
// Associate searchable configuration with the SearchView
SearchManager searchManager = (SearchManager)getSystemService(Context.SEARCH_SERVICE);
SearchView searchView = (SearchView) menu.findItem(R.id.search_menu).getActionView();
searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));
return true;
}
SearchResultsActivity.java:
public class SearchResultsActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("SearchResultsActivity", "onCreate called");
handleIntent(getIntent());
}
@Override
protected void onNewIntent(Intent intent) {
handleIntent(intent);
Log.d("SearchResultsActivity", "onNewIntent called");
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.d("SearchResultsActivity", query);
}
}
}
答案 0 :(得分:4)
也放
<meta-data android:name="android.app.default_searchable"
android:value=".SearchResultsActivity" />
在AndroidManifest中的<activity>
内。