0

В следующем SSCCE SearchView появляется дважды на Toolbar/ActionBar. Вопрос в том, почему!Почему SearchView дважды появляется на панели инструментов/панели действий?

enter image description here

SSCCE

MainActivity.java

public class MainActivity extends AppCompatActivity { 
    private TextView textView; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.activity_main); 
     textView = (TextView) findViewById(R.id.mainActivity_textView); 



     handleIntent(getIntent()); 
    } 

    @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); 
      doMySearch(query); 
     } 
    } 

    private void doMySearch(String searchQuery) { 
     textView.setText(searchQuery); 
    } 

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

     // Inflate the options menu from XML 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.main, menu); 

     // Get the SearchView and set the searchable configuration 
     SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); 
     MenuItem searchMenuItem = menu.findItem(R.id.action_search); 
     SearchView searchView = (SearchView) MenuItemCompat.getActionView(searchMenuItem); 
     // Assumes current activity is the searchable activity 
     searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); 
     //searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default 

     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(); 
     return super.onOptionsItemSelected(item); 
    } 
} 

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" > 

    <include android:id="@+id/mainActivity_toolbar" 
     layout="@layout/app_bar"/> 

    <TextView 
     android:id="@+id/mainActivity_textView" 
     android:layout_below="@id/mainActivity_toolbar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/hello_world" /> 

</RelativeLayout> 

app_bar.xml

<?xml version="1.0" encoding="utf-8"?> 
    <android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" > 
    </android.support.v7.widget.Toolbar> 

Рез/меню/main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:app="http://schemas.android.com/apk/res-auto" 
    xmlns:tools="http://schemas.android.com/tools" 
    tools:context="practice_projects.materialdesigngooglenowlikesearchviewgive.MainActivity" > 

    <item 
     android:id="@+id/action_search" 
     android:orderInCategory="100" 
     android:title="@string/searchMenuIcon_title" 
     android:icon="@drawable/ic_search_black_24dp" 
     app:showAsAction="always" 
     app:actionViewClass="android.support.v7.widget.SearchView" 
     app:iconifiedByDefault="false" /> 

</menu> 

Рез/XML/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> 

Рез/значение/styles.xml

<resources> 
    <style name="AppBaseTheme" parent="Theme.AppCompat.Light"> 
     <item name="colorPrimary">@color/primary</item> 
     <item name="colorPrimaryDark">@color/primaryDark</item> 
     <item name="colorAccent">@color/accent</item> 
    </style> 

    <style name="AppTheme" parent="AppBaseTheme"></style> 

</resources> 

Рез/значение * -v21 */styles.xml

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="AppTheme" parent="AppBaseTheme"></style> 
</resources> 

ответ

2

Поскольку вы раздувание ресурса меню дважды в onCreateOptionsMenu(). Удалите один из двух вызовов inflate().

1

Дважды? удалить первую строку

+0

sry didnt saw - CommonsWare answare –