为什么FindViewById()返回null

发布于 2025-02-07 23:28:35 字数 3162 浏览 1 评论 0原文

我正在尝试制作一个绘图应用程序,但是当我设置刷子尺寸并尝试进行文本视图以显示其尺寸时,它总是返回null,我尝试了多种方法,但它没有成功,所以请任何方法一个帮助我修复了此代码中的台词,因此文本显示了Seekbar Progress Text

class MainActivity : AppCompatActivity() {
        private var ibBrush: ImageView? = null
        private var tvBrushSize: TextView? = null
        private var sbBrushSize: SeekBar?= null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            ibBrush = findViewById(R.id.ib_brush)
            ibBrush?.setOnClickListener{
                changeBrushSize()
            }
    
    
        }
        private fun changeBrushSize(){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            val brushDialog = Dialog(this)
            brushDialog.setContentView(R.layout.brush_size_dialog)
            tvBrushSize = findViewById(R.id.tv_brush_size)
            sbBrushSize = findViewById(R.id.sb_brush_size)
    
            tvBrushSize?.text = sbBrushSize?.progress.toString()
    
    
            brushDialog.show()
    
    
        }
    }

为什么TVBrushSize在我将其设置为FindViewById()中返回null的原因是XML

* *这是XML *

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"

    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/brush_size"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/linear"/>

    <LinearLayout
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <SeekBar
            android:id="@+id/sb_brush_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="10"
            android:max="30"
            />

        <TextView
            android:id="@+id/tv_brush_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/_0" />
    </LinearLayout>
</LinearLayout>

I am trying to make a drawing app but when I am setting the brush size seek and try to make a text view to show its size it is always returning null I tried multiple ways but it didn't succeed so please can any one help me fix the poblems in this code so the text shows the seekbar progress text

class MainActivity : AppCompatActivity() {
        private var ibBrush: ImageView? = null
        private var tvBrushSize: TextView? = null
        private var sbBrushSize: SeekBar?= null
    
        override fun onCreate(savedInstanceState: Bundle?) {
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_main)
            ibBrush = findViewById(R.id.ib_brush)
            ibBrush?.setOnClickListener{
                changeBrushSize()
            }
    
    
        }
        private fun changeBrushSize(){
            AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
            val brushDialog = Dialog(this)
            brushDialog.setContentView(R.layout.brush_size_dialog)
            tvBrushSize = findViewById(R.id.tv_brush_size)
            sbBrushSize = findViewById(R.id.sb_brush_size)
    
            tvBrushSize?.text = sbBrushSize?.progress.toString()
    
    
            brushDialog.show()
    
    
        }
    }

Why tvBrushSize is returning null while I am setting it to findViewById() to the correct id in xml

*here is the xml *

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
    android:layout_height="match_parent"
    android:padding="10dp"
    android:orientation="vertical"

    >

    <TextView
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/brush_size"
        android:gravity="center"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintBottom_toTopOf="@+id/linear"/>

    <LinearLayout
        android:layout_width="350dp"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:orientation="horizontal"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView">

        <SeekBar
            android:id="@+id/sb_brush_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="10"
            android:max="30"
            />

        <TextView
            android:id="@+id/tv_brush_size"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:gravity="center"
            android:text="@string/_0" />
    </LinearLayout>
</LinearLayout>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

等你爱我 2025-02-14 23:28:35

您应该首先使用layoutinflater.inflate(layoutID)首先夸大对话框视图,然后使用view.findviewbyid(id)像此片段:

   private fun changeBrushSize(){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        val brushDialog = Dialog(this)
        val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
        brushDialog.setContentView(dialogView)
        tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
        sbBrushSize = dialogView.findViewById(R.id.sb_brush_size)

        tvBrushSize?.text = sbBrushSize?.progress.toString()


        brushDialog.show()


    }

You should inflate the dialog view first using LayoutInflater.inflate(layoutId) and then use view.findViewById(id) like this snippet:

   private fun changeBrushSize(){
        AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
        val brushDialog = Dialog(this)
        val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
        brushDialog.setContentView(dialogView)
        tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
        sbBrushSize = dialogView.findViewById(R.id.sb_brush_size)

        tvBrushSize?.text = sbBrushSize?.progress.toString()


        brushDialog.show()


    }
逆流 2025-02-14 23:28:35

您需要从对话框中绑定视图,因此请这样管理;

  brushDialog.setContentView(R.layout.brush_size_dialog)
  val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null) 
  brushDialog.setContentView(dialogView)
  tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)

you need to bind views from dialog, so manage it like this;

  brushDialog.setContentView(R.layout.brush_size_dialog)
  val dialogView = layoutInflater.inflate(R.layout.brush_size_dialog, null) 
  brushDialog.setContentView(dialogView)
  tvBrushSize = dialogView.findViewById(R.id.tv_brush_size)
眼前雾蒙蒙 2025-02-14 23:28:35

当您尝试使用FindViewById()引用它时,与您的视图的对话框尚不存在
您可以首先夸大视图,然后按以下方式访问

        val brushDialog = Dialog(this)
        val view = layoutInflater.inflate(R.layout.brush_size_dialog, null) 
        brushDialog.setContentView(view)
        tvBrushSize = view.findViewById(R.id.tv_brush_size)
        sbBrushSize = view.findViewById(R.id.sb_brush_size)
        tvBrushSize?.text = sbBrushSize?.progress.toString()
        brushDialog.show()

或调用brushdialog.show()首先,然后使用findViewById()访问视图,

该视图也应该有效

the dialog with your view is not yet existed, when you are trying to refer it with findViewById()
You can inflate view first, then access it as follows

        val brushDialog = Dialog(this)
        val view = layoutInflater.inflate(R.layout.brush_size_dialog, null) 
        brushDialog.setContentView(view)
        tvBrushSize = view.findViewById(R.id.tv_brush_size)
        sbBrushSize = view.findViewById(R.id.sb_brush_size)
        tvBrushSize?.text = sbBrushSize?.progress.toString()
        brushDialog.show()

or call brushDialog.show() first and then access views with findViewById()

that also should work

甩你一脸翔 2025-02-14 23:28:35

您应该先夸大视图

val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)

,然后尝试使用

dialogView.findViewById(...)

You should inflate the view first

val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)

then try to use

dialogView.findViewById(...)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文