So with the new Kotlin, Android studio update, it is impossible to call views using their respective ID like the norm, hence viewbinding. However, I have been trying to call the said view inside a function using the binding method to no success as it keeps returning an error
below is the code:
class JobActivity : AppCompatActivity() {
private val PROGRESS_MAX = 100
private val PROGRESS_START = 0
private val JOB_TIME = 4000 // ms
private lateinit var job: CompletableJob
private lateinit var binding: ActivityJobBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_job)
binding = ActivityJobBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.jobbutton.setOnClickListener {
if (!::job.isInitialized ){
initjob()
}
}
}
fun initjob(){
binding.jobbutton.setText("StartJob #1")
binding.textView2.setText("")
job= Job()
job.invokeOnCompletion {
it?.message.let {
var msg = it
if (msg.isNullOrBlank()){
msg ="Uknown cancellation Error"
}
println("${job} was cancelled. Reason:$msg")
showtoast(msg)
}
}
binding.progressBar.max= PROGRESS_MAX
binding.progressBar.progress= PROGRESS_START
}
fun showtoast(text:String){
Toast.makeText(this@JobActivity, text,Toast.LENGTH_SHORT).show()
}
}
fun ProgressBar.startJobOrCancel(job: Job) {
if (this.progress > 0) {
Log.d(TAG, "${job} is already active. Cancelling...")
resetjob()
} else {
binding.jobbutton.setText("StartJob #1")
Log.d(TAG, "coroutine ${this} is activated with job ${job}.")
}
}
the error occurs here:
please provide precise steps
