can't find textView id inside toolbar

Viewed 1056

I am working with a simple application on android where at the moment i net to create a toolbar so i created a custom toolbar like this:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/my_toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorPrimary">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@color/white"
        android:layout_gravity="center"
        android:id="@+id/toolbar_title"
        />
</android.support.v7.widget.Toolbar>

then i included this custom layout on my activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:fitsSystemWindows="true"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.afcosta.inesctec.pt.android.FamilyLibrary">


    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|end"
        android:layout_margin="@dimen/fab_margin"
        app:srcCompat="@android:drawable/ic_menu_camera"
        app:backgroundTint="#f1c40f"
        tools:layout_constraintRight_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toBottomOf="parent"
        android:layout_marginEnd="37dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginBottom="33dp" />

    <android.support.v7.widget.RecyclerView
        android:id="@+id/gallery"
        android:layout_width="0dp"
        android:layout_height="562dp"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginStart="8dp"
        android:layout_marginEnd="8dp"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="97dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <include
        layout="@layout/custom_toolbar"
        android:id="@+id/toolbar"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="@+id/textView3"
        app:layout_constraintTop_toTopOf="parent" />


    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Familias"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/gallery"
        android:layout_marginStart="8dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="15dp"
        app:layout_constraintLeft_toLeftOf="parent"
        android:textSize="24dp"/>

when i try to setText on my activity for the TextView, i fails and i get this error: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v7.widget.Toolbar.findViewById(int)' on a null object reference

here is my code on the activity:

  Toolbar myToolbar = (Toolbar) findViewById(R.id.my_toolbar);;
        setSupportActionBar(myToolbar);
        TextView toolbarText = (TextView) myToolbar.findViewById(R.id.toolbar_title);
        toolbarText.setText("Famílias");

Any tip?

Thank you very much!

2 Answers
Related