java - 在 Kotlin 中以编程方式从 fragment 更改工具栏文本

标签 java android android-fragments kotlin

我有一个以 Fragments 为主要内容的 Activity。 此 Activity 有一个工具栏,里面有一个 TextView:

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(my_activity)

    val toolbar = findViewById<Toolbar>(R.id.toolbar)
    setSupportActionBar(toolbar)
    supportActionBar!!.setDisplayHomeAsUpEnabled(false)
    supportActionBar!!.setDisplayShowTitleEnabled(false)
}

这是该 Activity 的 xml 文档中的工具栏:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MyActivity">

    <include
        android:id="@+id/toolbar"
        layout="@layout/my_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

        <fragment android:name="org.siku.siku.MyFragment"
            android:id="@+id/fullscreen_content"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            layout="@layout/my_fragment" />

    </FrameLayout>
</android.support.constraint.ConstraintLayout>

这是我正在使用的toolbar.xml:

<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <Button
            android:id="@+id/closeBtn"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:id="@+id/titleLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <TextView
                android:id="@+id/toolBarText1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"/>
        </LinearLayout>
    </android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>

我可以轻松地从该 Activity 更改文本,但我不可能更改 fragment 中的文本:

我尝试从 Fragment 访问 TextView,但收到错误

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?): View? {
    myView = inflater.inflate(R.layout.my_frament, container, false)
    val toolbar = activity!!.findViewById<Toolbar>(R.id.toolbar)
    val toolbarTextView = toolbar.findViewById<TextView>(R.id.toolBarText1)

}

这是我收到的错误:

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

错误来自这一行: valtoolbarTextView =toolbar.findViewById(R.id.toolBarText1)

这意味着我没有正确检索 TextView,但从我看到的示例来看,这应该是正确的方法。

最佳答案

您必须等待Activity创建:

override fun onActivityCreated(savedInstanceState: Bundle?) {
    super.onActivityCreated(savedInstanceState)

    activity.actionBar.title = ""
}

因为您在 Activity 中执行了 setSupportActionBar(...),所以 getActionBar() 应该null

编辑: 如果您想使用自定义标题 TextView,您的 findViewById() 方法也应该有效。重要的是等待 Activity 创建。

关于java - 在 Kotlin 中以编程方式从 fragment 更改工具栏文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52283564/

相关文章:

android - ListView 行中的 Google map API V2

Android - android.view.InflateException : Binary XML file line #8: Error inflating class fragment

java - J2ME : UI framework

java - 优化具有多个循环的方法

android - 尝试在适配器内添加 fragment

android - 相机 Intent 不保存照片

android - 任何带有 NEON 指令集的 Android 平板电脑?

java - 如何将 HTML/Javascript 代码转换为可用于 Eclipse 中的 Android 应用程序

Java Pattern.matches() 因长字符串而失败

Java 类 类