So I am brand new to coding and I’m making an app in Android Studio
in Kotlin
and on the homepage of my app I have a button that is supposed to take me to another activity
and every time I click on it, the app crashes. I want to know how to fix this, and if it is a code error or a memory error. Thanks in advance!
Here’s the code:
package com.smvcalculator
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val MaleLP = findViewById<Button>(R.id.malebtn)
MaleLP.setOnClickListener {
val intent = Intent(this, MaleLP::class.java)
startActivity(intent)
}
}
}
Logcat Error:
2021-07-14 14:51:57.879 9421-9421/com.smvcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.smvcalculator, PID: 9421
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.smvcalculator/com.google.android.material.button.MaterialButton}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2065)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1727)
at android.app.Activity.startActivityForResult(Activity.java:5320)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:574)
at android.app.Activity.startActivityForResult(Activity.java:5278)
at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:560)
at android.app.Activity.startActivity(Activity.java:5664)
at android.app.Activity.startActivity(Activity.java:5617)
at com.smvcalculator.MainActivity.onCreate$lambda-0(MainActivity.kt:15)
at com.smvcalculator.MainActivity.lambda$pO9IRxlIimBerH3k2cIfwv6P3Wg(Unknown Source:0)
at com.smvcalculator.-$$Lambda$MainActivity$pO9IRxlIimBerH3k2cIfwv6P3Wg.onClick(Unknown Source:4)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
2021-07-14 14:51:58.073 9421-9421/com.smvcalculator I/Process: Sending signal. PID: 9421 SIG: 9
2
Answers
I finally found the error. Your
Button
has the same name like theActivity
that will be used forintent
. Change it to this:Now it should work 🙂
I guess the issue here is, it says that
have you declared this activity in your AndroidManifest.xml?
So you need to add this activity’s name in yourAndroidManifest.xml
file, i.e.MaleLP.java
like this: