为什么findViewById()返回null



我正在尝试制作一个绘图应用程序,但是当我设置笔刷大小寻求并尝试制作一个文本视图来显示其大小时它总是返回null我尝试了多种方法但没有成功所以请任何人帮助我解决这个代码中的问题所以文本显示搜索栏进度文本

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返回null,而我将其设置为findViewById()到xml中的正确id

*这里是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>

您应该首先使用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()

}

你需要从对话框中绑定视图,所以像这样管理它;

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)

与视图的对话框还不存在,当您试图用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()访问视图

也可以

应该先对视图进行充气

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

然后尝试使用

dialogView.findViewById(...)

最新更新