onSearchRequested 未启动 search_activity
onSearchRequested does not start search_activity
我想在单击按钮时开始搜索 activity,现在 oncreate()
没有被调用。我怀疑我的 AndroidManifest.xml
有误,但找不到它..
Main_Activity
内的按钮点击事件
public void search(View view) {
Log.d("search", "start searchButton"); //this is shown in logcat
onSearchRequested();
}
AndroidManifest.xml
<!--Main activity.-->
<activity
android:name=".Main_Activity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".Search_Activity" />
</activity>
<!--Activity and metadata for search activity. -->
<activity
android:name=".Search_Activity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter >
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
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"
</searchable>
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/searchList" />
</LinearLayout>
Search_Activity.java
public class Search_Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("search", "start search"); //does not show in logcat!!!!!!!!!!
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
handleIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l,
View v, int position, long id) {
// call detail activity for clicked entry
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query =
intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
// get a Cursor, prepare the ListAdapter
// and set it
}
}
有人看错了吗?或者关于如何找出问题所在的任何想法?
您应该使用 Intent
开始一个新的 activity。试试这个:
public void search(View view) {
Log.d("search", "start searchButton"); //this is shown in logcat
startActivity(new Intent(this, Search_Activity.class));
}
我想在单击按钮时开始搜索 activity,现在 oncreate()
没有被调用。我怀疑我的 AndroidManifest.xml
有误,但找不到它..
Main_Activity
public void search(View view) {
Log.d("search", "start searchButton"); //this is shown in logcat
onSearchRequested();
}
AndroidManifest.xml
<!--Main activity.-->
<activity
android:name=".Main_Activity"
android:label="@string/app_name"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.default_searchable"
android:value=".Search_Activity" />
</activity>
<!--Activity and metadata for search activity. -->
<activity
android:name=".Search_Activity"
android:label="@string/app_name"
android:launchMode="singleTop" >
<intent-filter >
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data
android:name="android.app.searchable"
android:resource="@xml/searchable" />
</activity>
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"
</searchable>
search_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/searchList" />
</LinearLayout>
Search_Activity.java
public class Search_Activity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
Log.d("search", "start search"); //does not show in logcat!!!!!!!!!!
super.onCreate(savedInstanceState);
setContentView(R.layout.search_layout);
handleIntent(getIntent());
}
@Override
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l,
View v, int position, long id) {
// call detail activity for clicked entry
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query =
intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
// get a Cursor, prepare the ListAdapter
// and set it
}
}
有人看错了吗?或者关于如何找出问题所在的任何想法?
您应该使用 Intent
开始一个新的 activity。试试这个:
public void search(View view) {
Log.d("search", "start searchButton"); //this is shown in logcat
startActivity(new Intent(this, Search_Activity.class));
}