0

Ich habe viele ähnliche Themen gelesen, aber keine der Lösungen funktioniert für mich. Ich bekomme das MenuItem normalerweise, aber wenn ich versuche, seine Ansicht zu bekommen, bekomme ich eine Null Ansicht. Ich habe bereits versucht, den Code in die onPrepareOptionsMenu, erhalten Sie die Ansicht mit MenuItemCompat, und andere sutff.Null MenüItem-Ansicht von einer benutzerdefinierten Symbolleiste

activity_main.xml, wo der Werkzeugleiste

<?xml version="1.0" encoding="utf-8"?> 
<android.support.design.widget.CoordinatorLayout 
    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=".activities.MainActivity"> 

    <android.support.design.widget.AppBarLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:theme="@style/AppTheme.AppBarOverlay"> 

     <android.support.v7.widget.Toolbar 
      android:id="@+id/toolbar" 
      android:layout_width="match_parent" 
      android:layout_height="?attr/actionBarSize" 
      android:background="?attr/colorPrimary" 
      app:popupTheme="@style/AppTheme.PopupOverlay" 
      android:elevation="4dp"/> 

    </android.support.design.widget.AppBarLayout> 
    ... 
</android.support.design.widget.CoordinatorLayout> 

menu_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=".activities.MainActivity"> 

    <item 
     android:id="@+id/action_location" 
     android:orderInCategory="0" 
     android:icon="@android:drawable/ic_dialog_map" 
     android:title="Localização" 
     app:showAsAction="ifRoom" /> 

    ... 
</menu> 

onCreate Methode

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

    setContentView(R.layout.activity_main); 
    toolbar = (Toolbar)findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    ... 
} 

onCreateOptionsMenu Methode

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.menu_main, menu); 

    MenuItem menuItem = menu.findItem(R.id.action_location); // here is fine, found the item 
    View view = menuItem.getActionView(); // here is null 

    return super.onCreateOptionsMenu(menu); 
} 
ist

Antwort

0

Machen Sie sich mit dem vertraut, was ein ActionView wirklich ist.

Nur durch Zuweisen menuItem, erhalten Sie alle Attribute darüber, einschließlich des Titels "Localização."

Wenn Sie versuchen, das Ergebnis von Klick "action_location" zu erhalten, dann kann onOptionsItemSelected der Methodenaufruf sein, den Sie benötigen.

Verwandte Themen