I decided to learn Android Developing, and after some time it came to create first app, that does something (even if creation process is just a copy from YouTube lesson. Fast changing technologies quickly loose their relevance to those free online lessons, so my app doesn't work, and I don't know where the problem and how to fix it. My app is a simple calculator. When I decided to try if just one button works, app crashed in AVD. My current Android Studio is Jellyfish 2023.3.1 Here's code:
package com.example.calculator
import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}
btn_0.setOnClickListener{setTextFields("0")}
}
val btn_0: TextView = findViewById(R.id.btn_0)
var math_operation: TextView = findViewById(R.id.math_operation)
fun setTextFields(str: String){
math_operation.append(str)
}
}
also xml part, where is the tested button
<LinearLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:orientation="horizontal">
<TextView
style="@style/Number_Button"
android:text="."
android:id="@+id/btn_dot"
/>
<TextView
style="@style/Number_Button"
android:text="0"
android:id="@+id/btn_0"
/>
<TextView
style="@style/Action_Button"
android:text="Back"
android:id="@+id/btn_back"
/>
<TextView
style="@style/Action_Button"
android:text="="
android:id="@+id/btn_eq"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Then, when I searched some, I came to Logcat, and found that:
FATAL EXCEPTION: main Process: com.example.calculator, PID: 13069 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.calculator/com.example.calculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
Also, if you need, a design screen.enter image description here
Pls, helppppp.
I've done some search on the internet, but don't quite get what to do, just where to find error description
Experimentally I've discovered, that try-to-output-number-in-math_operation_textview-experiment is successfully ended with one little change:
var math_operation: TextView = findViewById(R.id.math_operation)
should go inside the fun setTextFields()
, and shouldn't be outside of any function. I've tried to create open var math_operation
inside onCreate()
, but Android Studio doesn't allow me to do this. Also, I've moved var btn_0 ...
in onCreate()
, right above btn_0.setOnClickListener()
, because the same error occured in that line, according to Logcat. I will continue to work on this app untill it works, and will share with you my results