按下搜索按钮时不会触发onCreate()或onNewIntent()

时间:2011-03-02 03:39:39

标签: android search android-intent

我正在尝试在我的应用中实现搜索功能,这在目前非常基础。当我按下Nexus上的搜索按钮时,搜索意图似乎没有触发,因为onCreate()和onNewIntent()都没有被调用。我基本上复制了你在Android developers上可以找到的整个例子,但它仍然没有用。谢谢你的帮助

RES / searchable.xml

<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
    android:label="hello"
    android:hint="Search lectures" >
</searchable>

的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.openday.lectures"
      android:versionCode="1"
      android:versionName="1.0">
    <uses-sdk android:minSdkVersion="7" />

    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".LecturesListActivity"
                  android:label="@string/app_name"
                  android:launchMode="singleTop">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />            
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEARCH" />
            </intent-filter>
            <meta-data android:name="android.app.searchable"
                       android:resource="@xml/searchable"/>
        </activity>
        <activity android:name=".SingleLectureActivity"
                  android:label="Lecture">
        </activity>
    </application>

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

LecturesListActivity.java(缩写)

public class LecturesListActivity extends ListActivity {

    private String categoryDisplayed;
    private String facultyDisplayed;
    private List<Lecture> lectures;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        loadLectures();
        displayFaculties();
    }

    @Override
    protected void onNewIntent(Intent intent) {
        setIntent(intent);
        handleIntent(intent);
    }

    private void handleIntent(Intent intent) {
        if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
          String query = intent.getStringExtra(SearchManager.QUERY);
          searchForLectures(query);
        }
    }
}

1 个答案:

答案 0 :(得分:4)

searchable.xml中的常量字符串问题,答案与this question

相同