-1

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.

Shark
  • 6,513
  • 3
  • 28
  • 50
MrE
  • 3
  • 1
  • 1
    Please post the error message. If it's an exception, please include the stack trace. – gidds May 24 '22 at 11:45
  • A window popps up "FinalWaterCounter keeps stopping" that's it, no error or something in the code – MrE May 24 '22 at 11:52
  • There will very probably be a full exception message, with a stack trace, in the logs. If you don't know how to get hold of that, hopefully someone else will be along to explain how. (I don't do Android development…) That exception will very probably tell you exactly what's wrong with your code. – gidds May 24 '22 at 11:55
  • In Android Studio Under Logcat (available in the footer section) you can find an error log message. Please provide with the error message – Kartik Agarwal May 24 '22 at 12:01
  • 1
    Does this answer your question? [Unfortunately MyApp has stopped. How can I solve this?](https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) – a_local_nobody May 24 '22 at 12:16

2 Answers2

1

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.

Kartik Agarwal
  • 1,129
  • 1
  • 8
  • 27
Abu bakar
  • 751
  • 1
  • 4
  • 16
1

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()
    ...
}
Roland
  • 91
  • 4