【问题标题】:can't find textView id inside toolbar在工具栏中找不到 textView id
【发布时间】:2017-10-31 09:34:44
【问题描述】:

我正在使用 android 上的一个简单应用程序,目前我正在创建一个工具栏,所以我创建了一个这样的自定义工具栏:

<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>

然后我在我的活动中加入了这个自定义布局:

<?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"/>

当我尝试在我的 TextView 活动上设置文本时,我失败了,我收到了这个错误: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

这是我的活动代码:

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

有什么建议吗?

非常感谢!

【问题讨论】:

    标签: java android xml android-layout


    【解决方案1】:

    您为工具栏布局提供了两个不同的 id

     android:id="@+id/my_toolbar"
    

    在主布局中,包括你正在给予

     <include
        layout="@layout/custom_toolbar"
        android:id="@+id/toolbar" />
    

    使这些 id 相同

    【讨论】:

    • 不错的收获,今天的决赛,无法抓住:/,非常感谢!
    【解决方案2】:

    如果我没记错的话,你不需要在工具栏上调用findViewById(),你应该可以使用这个:findViewById(R.id.toolbar_title)

    您的代码最终应如下所示:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-20
      • 1970-01-01
      • 2015-07-25
      • 1970-01-01
      相关资源
      最近更新 更多