Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

can anyone help me out why I am getting null value even there is no error in the code

package com.example.myquizapp
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.ProgressBar
import android.widget.TextView

here is my code

class QuizActivity : AppCompatActivity() {
    private var progressBar : ProgressBar?= null
    private var tvProgress : TextView? = null

    override fun onCreate(savedInstanceState: Bundle?) { 
            progressBar = findViewById(R.id.progressBar)  

on hover R.id.progressBar I get :public static final int progressBar = 1000004

            tvProgress = findViewById(R.id.tv_progress)

// public static final int tv_progress = 100000

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

//on hover progressBar I get : private final var progressBar: ProgressBar?

            super.onCreate(savedInstanceState)
            setContentView(R.layout.activity_quiz)

            var currentPosition = 1
            progressBar?.progress=currentPosition                       
            tvProgress?.text="${currentPosition}/${progressBar?.max}"
            Log.e("currentPosition" "${currentPosition})        // 1
            Log.e("ProgressBar", "${progressBar}")    // null, getting null value on logcat
            Log.e("TvOrogress", "${tvProgress}")// null, getting null value on logcat
            Log.e("tvProgress",${tvProgress?.text.toString}  // null

    }
}

>Solution :

Move the findViewById() calls after setContentView().

setContentView() inflates the view hierarchy and adds it to the activity. findViewById() looks up view in the activity view hierarchy, and it is empty before setContentView() so findViewById() returns null.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading