Toolbar - findViewbyID null zurückgeben

Warum bin ich nicht in der Lage zu finden, meine Toolbar in mein layout?

setContentView(R.layout.activity_main);

toolbar = (Toolbar) findViewById(R.id.toolbar);
if (toolbar != null) {
    setSupportActionBar(toolbar);
    getSupportActionBar().setHomeButtonEnabled(true);
}

Danach toolbar ist immer noch null.

activity_man.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipToPadding="false"
    tools.context=".PlayerActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/toolbar"/>
    <ImageView
        android:id="@+id/background_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scaleType="centerCrop"/>
        : (goes on)

EDIT:

toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"/>

Dank!

  • Was ist in res/layout/toolbar.xml? R.id.toolbar verweist auf eine <include>, nicht ein Toolbar. "In meiner Symbolleiste layout gibt 's nur die Toolbar" - aber das layout ist nicht Teil deiner Frage, und so können wir seinen Inhalt anzuzeigen.
  • Verwenden Sie setContentView für toolbar.xml?
  • Aktualisiert mit setcontenview und layout. Ich habe schon ein bisschen rumprobiert und jetzt funktioniert ' s aber ist die Symbolleiste nicht auf dem Bildschirm sichtbar?!
InformationsquelleAutor diiiz_ | 2014-12-14
Schreibe einen Kommentar