-1

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
Ole Pannier
  • 3,208
  • 9
  • 22
  • 33

2 Answers2

1

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 your AndroidManifest.xml file, i.e. MaleLP.java like this:

<activity
        android:name=".MaleLP"
       />
mehul bisht
  • 682
  • 5
  • 15
0

I finally found the error. Your Button has the same name like the Activity that will be used for intent. Change it to this:

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_button = findViewById<Button>(R.id.malebtn) //new button name
        maleLP_button.setOnClickListener {
            val intent = Intent(this, MaleLP::class.java) //different to class name
            startActivity(intent)
        }
    }
}

Now it should work :)

Ole Pannier
  • 3,208
  • 9
  • 22
  • 33
  • Did the first suggestion, haven't ran the code yet but what is the benefit of doing the second thing??? Where you said "And I bet you forget to define the new Activity into your AndroidManifest.xml. So add this piece of code above your other " –  Jul 14 '21 at 18:57
  • You need to define that another activity is there. Without that it can not be initialized. Did you try to add it? – Ole Pannier Jul 14 '21 at 18:58
  • Didn't work, this is what I got. .......\SMVCalculator\app\src\main\java\com\smvcalculator\MainActivity.kt: (14, 33): Unresolved reference: context –  Jul 14 '21 at 19:03
  • and yeah I did, I made the variable and referenced the activity, maybe I didn't do it right.class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val MaleLP = findViewById –  Jul 14 '21 at 19:05
  • So you defined both classes in your AndroidManifest.xml? – Ole Pannier Jul 14 '21 at 19:10
  • I found the error. I updated my answer. Take a look. I rebuild your app and it worked for me. – Ole Pannier Jul 14 '21 at 19:22
  • Thank you so much! So it was because of conflicting names with the same names? –  Jul 14 '21 at 19:36
  • Exactly! So it gave a compilation error. Tick the question correct (hook) to help others facing the same error. Cheers! :) – Ole Pannier Jul 14 '21 at 19:38