为什么FindViewById()返回null
我正在尝试制作一个绘图应用程序,但是当我设置刷子尺寸并尝试进行文本视图以显示其尺寸时,它总是返回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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您应该首先使用
layoutinflater.inflate(layoutID)
首先夸大对话框视图,然后使用view.findviewbyid(id)
像此片段:You should inflate the dialog view first using
LayoutInflater.inflate(layoutId)
and then useview.findViewById(id)
like this snippet:您需要从对话框中绑定视图,因此请这样管理;
you need to bind views from dialog, so manage it like this;
当您尝试使用FindViewById()引用它时,与您的视图的对话框尚不存在
您可以首先夸大视图,然后按以下方式访问
或调用
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
or call
brushDialog.show()
first and then access views withfindViewById()
that also should work
您应该先夸大视图
,然后尝试使用
You should inflate the view first
then try to use