2015-05-04 158 views
0

我試圖在搜索欄中使用搜索視圖添加搜索活動。我見過這樣的幾個問題,但我已經嘗試了所有的答案,沒有爲我工作。搜索顯示正確,但當我輸入搜索短語並點擊搜索後,什麼也沒有發生。 我的清單SearchView未啓動搜索活動

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.y2apps.quoteformessenger.app" 
    android:versionCode="1" 
    android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="15" 
    android:targetSdkVersion="21" /> 

<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:resource="@xml/searchable" android:value=".SearchResultsActivity"/> 

    <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> 
     <meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" /> 
    </activity> 

    <activity 
     android:name=".SearchResultActivity" 
     android:theme="@style/AppTheme" 
     android:launchMode="singleTop"> 
    <intent-filter> 
     <action android:name="android.intent.action.SEARCH" /> 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 
       <meta-data android:name="android.app.searchable" android:resource="@xml/searchable" /> 
    </activity> 
</application> 

的searachable.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" /> 

的styles.xml文件

<!-- 
    Base application theme, dependent on API level. This theme is replaced 
    by AppBaseTheme from res/values-vXX/styles.xml on newer devices. 
--> 
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
    <!-- 
     Theme customizations available in newer API levels can go in 
     res/values-vXX/styles.xml, while customizations related to 
     backward-compatibility can go here. 
    --> 
</style> 

<!-- Application theme. --> 
<style name="AppTheme" parent="Theme.AppCompat.Light"> 
    <!-- All customizations that are NOT specific to a particular API-level can go here. --> 
</style> 

主要活動

public class MainActivity extends ActionBarActivity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     getActionBar().setDisplayShowTitleEnabled(false); 
     getActionBar().setDisplayShowHomeEnabled(false); 
    } 

    @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); 

     // Associate searchable configuration with the SearchView 
     SearchManager searchManager = 
       (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     SearchView searchView = 
       (SearchView) menu.findItem(R.id.search).getActionView(); 
     if (searchView != null) { 
      searchView.setSearchableInfo(
        searchManager.getSearchableInfo(getComponentName())); 
      searchView.setIconifiedByDefault(false); 
     } else { 
      Toast.makeText(this, "can't find search bar", Toast.LENGTH_LONG).show(); 
     } 

     return true; 
    } 

    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 
     if (id == R.id.action_settings) { 
      return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 
} 

和搜索活動

public class SearchResultActivity extends Activity { 
    // =========================================================== 
    // Public Constants 
    // =========================================================== 

    // =========================================================== 
    // Private constants and Fields 
    // =========================================================== 

    // =========================================================== 
    // Constructors 
    // =========================================================== 

    // =========================================================== 
    // Public methods 
    // =========================================================== 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     handleIntent(getIntent()); 
    } 
    // =========================================================== 
    // Private methods 
    // =========================================================== 
    @Override 
    protected void onNewIntent(Intent intent) { 
     handleIntent(intent); 
    } 

    private void handleIntent(Intent intent) { 
     Toast.makeText(this, "YARON YOREH 2", Toast.LENGTH_LONG).show(); 
     if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
      String query = intent.getStringExtra(SearchManager.QUERY); 
      Toast.makeText(this, "YARON YOREH asked for " + query, Toast.LENGTH_LONG).show(); 
      //use the query to search your data somehow 
     } 
    } 
    // =========================================================== 
    // Inner and Anonymous Classes 
    // =========================================================== 

} 

如果它可以幫助診斷這一點,搜索提示沒有顯示出來無論是。 感謝您的幫助

回答

0

您的清單一個錯字:

<meta-data android:name="android.app.default_searchable" android:value=".SearchActivity" /> 

我相信它應該是:

<meta-data android:name="android.app.default_searchable" android:value=".SearchResultsActivity"/> 

但是,你不需要因爲你已經定義默認全球搜索您的應用程序

+0

謝謝你馬克。這解決了問題 – yaronbic

0

根據你的代碼,我明白你的MainActivity是搜索活動,你想顯示把結果放在新的活動中。爲此,您必須將MainActivty設置爲可搜索。更改您的清單,如下所示。 主要活動進行搜索

 <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> 
      <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=".SearchResultActivity" ... > 

     <meta-data android:name="android.app.default_searchable" 
        android:value=".MainActivity" /> 
    </activity> 
+0

謝謝Gautam爲您解答,但我的主要活動是擁有搜索欄和搜索活動的主要活動是我的搜索活動。我的問題是如下所述的錯字。但是,感謝您花時間閱讀它 – yaronbic