I just made this code but all of the sudden the application crashes immediately. This is supposedly a water counter.When I first tried to open the application, it just delivered me an error message.
This never happened before, I hope some of you can help me?
You can find the code lines and design files below. You can examine in detail.
my Main.kt
file:
package com.example.finalwatercounter
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ProgressBar
import android.widget.TextView
class MainActivity : AppCompatActivity() {
private var progr = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val button_plus = findViewById<Button>(R.id.button_plus)
val button_minus = findViewById<Button>(R.id.button_minus)
updateProgressBar()
button_plus.setOnClickListener{
if(progr <= 9)
{
progr =1
updateProgressBar()
}
}
button_minus.setOnClickListener{
if(progr >= 1)
{
progr -=1
updateProgressBar()
}
}
}
val progress_bar = findViewById<ProgressBar>(R.id.progress_bar)
var text_view_progress = findViewById<TextView>(R.id.text_view)
private fun updateProgressBar()
{
progress_bar.progress = progr
text_view_progress.text = "$progr" " cups"
}
}
my main.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ProgressBar
android:id="@ id/progress_bar"
android:layout_width="200dp"
android:layout_height="200dp"
android:indeterminateOnly="false"
android:progressDrawable="@drawable/circle"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.508"
tools:progress="60" />
<TextView
android:id="@ id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toBottomOf="@ id/progress_bar"
app:layout_constraintEnd_toEndOf="@ id/progress_bar"
app:layout_constraintStart_toStartOf="@ id/progress_bar"
app:layout_constraintTop_toTopOf="@ id/progress_bar"
tools:text="60%" />
<Button
android:id="@ id/button_minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="-"
app:layout_constraintStart_toStartOf="@ id/progress_bar"
app:layout_constraintTop_toBottomOf="@ id/progress_bar" />
<Button
android:id="@ id/button_plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text=" "
app:layout_constraintEnd_toEndOf="@ id/progress_bar"
app:layout_constraintTop_toBottomOf="@ id/progress_bar" />
</androidx.constraintlayout.widget.ConstraintLayout>
my circle.xml
file:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="false">
<solid android:color="#DDD" />
</shape>
</item>
<item>
<rotate
android:fromDegrees="270"
android:toDegrees="270 ">
<shape
android:shape="ring"
android:thicknessRatio="16"
android:useLevel="true">
<solid android:color="#FFF34234"/>
</shape>
</rotate>
</item>
</layer-list>
Can you help me, please? How can I solve this error?
Thanks for helping.
CodePudding user response:
You need to write these two lines in updateProgressBar()
:
val progress_bar = findViewById<ProgressBar>(R.id.progress_bar)
var text_view_progress = findViewById<TextView>(R.id.text_view)
where button_plus
and button_minus
are initialized.
Use Logcat
or Run
tab to find the exception. These buttons are located at the bottom of your IDE.
CodePudding user response:
The problem with your code is that when onCreate
is called, updateProgressBar()
is called before the variables in it are initialized.
So, to solve the issue, initialize the variables called in updateProgressBar
(that is, progress_bar
and text_view_progress
) in onCreate
before calling the function.
Like so:
private lateinit var text_view_progress: TextView
private lateinit var progress_bar: ProgressBar
override fun onCreate(savedInstanceState: Bundle?) {
...
progress_bar = findViewById(R.id.progress_bar)
text_view_progress = findViewById(R.id.text_view)
updateProgressBar()
...
}