Home > Net >  Why findViewById() is returning null
Why findViewById() is returning null

Time:06-15

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>

CodePudding user response:

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

CodePudding user response:

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)

CodePudding user response:

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()


    }

CodePudding user response:

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(...)
  • Related