我正在嘗試制作一個繪圖應用程式,但是當我設定畫筆大小尋求并嘗試制作一個文本視圖以顯示其大小時,它總是回傳 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 在我將其設定為 findViewById() 時回傳 null 到 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>
uj5u.com熱心網友回復:
您應該首先使用膨脹對話框視圖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()
}
uj5u.com熱心網友回復:
帶有您的視圖的對話框尚不存在,當您嘗試使用 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()
那也應該作業
uj5u.com熱心網友回復:
你需要從對話框系結視圖,所以像這樣管理它;
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)
uj5u.com熱心網友回復:
你應該先膨脹視圖
val dialogView = LayoutInflater.inflate(R.layout.brush_size_dialog, null)
brushDialog.setContentView(dialogView)
然后嘗試使用
dialogView.findViewById(...)
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/492122.html
標籤:安卓 xml 科特林 无效的 findviewbyid
下一篇:選擇匹配名稱的XML節點
