3

我已经设置了可搜索的配置,但现在我遇到了奇怪的问题。当我提交搜索查询时,SearchActivity.class会启动一秒钟,然后返回MainActivity.class。我不知道,会发生什么。

查看我的代码:

MainActivity

@Override
        public boolean onCreateOptionsMenu(Menu menu) {

         // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE);

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

            if (null != mSearchView )
            {
                mSearchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName()));

            }

            SearchView.OnQueryTextListener queryTextListener = new SearchView.OnQueryTextListener() 
            {
                public boolean onQueryTextChange(String newText) 
                {


                    return true;
                }

                public boolean onQueryTextSubmit(String query) 
                {
                    String said = query.toString();
                    Log.v(said, "submited");
                    Intent intent = new Intent(getApplicationContext(), SearchActivity.class);
                    intent.setAction(Intent.ACTION_SEARCH);
                    intent.putExtra("query", query);
                    startActivity(intent);

                    return true;

                }
            };
            mSearchView.setOnQueryTextListener(queryTextListener);

            return super.onCreateOptionsMenu(menu);
        }

安卓清单

<application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.animalist.MainActivity"
            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>

           <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="com.example.animalist.MoreActivity"
            android:label="@string/title_activity_more"
            android:launchMode="singleTop"
            android:parentActivityName="com.example.animalist.MainActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.animalist.MainActivity" />
        </activity>

        <activity
            android:name="com.example.animalist.SearchActivity"
            android:label="@string/title_activity_search"
            android:launchMode="singleTop"
            android:parentActivityName="com.example.animalist.MainActivity"
            android:screenOrientation="portrait" >
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value="com.example.animalist.MainActivity" />

        </activity>

    </application>

我究竟做错了什么?

4

1 回答 1

1

基于这个线程,它似乎是Android的一个错误。

从这里(日语)找到这个临时的“解决方案” 。这个对我有用。

@Override
public boolean onQueryTextSubmit(String query) {
    mSearchView.clearFocus();

    ......
    // your code to process the query
    ......

    return false;
}
于 2014-09-25T00:57:39.340 回答