2015-06-06 50 views
0

我試圖在ActionBar中創建一個SearchView。我起初使用this作爲我的資源,但我嘗試過,當我在手機中啓動應用程序時,它說它意外退出。我試圖使用this,但代碼也沒有工作。ActionBar SearchView無法正常工作

// This is my java code 
    @Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.menu_results, menu); 

    SearchManager searchManager = 
      (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
    SearchView searchView = 
      (SearchView) menu.findItem(R.id.search).getActionView(); 
    searchView.setSearchableInfo(
      searchManager.getSearchableInfo(getComponentName())); 

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

    //noinspection SimplifiableIfStatement 
    if (id == R.id.action_settings) { 
     return true; 
    } 

    return super.onOptionsItemSelected(item); 
} 


private void handleIntent(Intent intent) { 

    if (Intent.ACTION_SEARCH.equals(intent.getAction())) { 
     String query = intent.getStringExtra(SearchManager.QUERY); 
     //use the query to search your data somehow 
    } 
} 


//This is my AndroidManifest.xml 
<activity 
     android:name=".results" 
     android:label="@string/title_activity_results" > 
     <meta-data android:name="android.app.searchable" 
      android:resource="@xml/searchable" /> 
     <intent-filter> 
      <action android:name="android.intent.action.SEARCH" /> 
     </intent-filter> 
    </activity> 

我有一個searchable.xml文件。

謝謝。

回答

0

確保您使用的是v4支持庫的searchview。

+0

我嘗試了v4和v7,但沒有奏效。 – taeuk