androidcrashfunction-declarationexplicitactivitynotfoundexception

Android Studio - "Unable to find explicit activity class" and I have already declared the activities in Manifest, which is the suggested problem


This error comes up in the logcat when I try to click on a button in my application and it crashes:

```
FATAL EXCEPTION: main
Process: com.example.savedtrial, PID: 16090
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.savedtrial/com.google.android.material.button.MaterialButton}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared <intent-filter>?
    at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2197)
    at android.app.Instrumentation.execStartActivity(Instrumentation.java:1839)
    at android.app.Activity.startActivityForResult(Activity.java:5471)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728)
    at android.app.Activity.startActivityForResult(Activity.java:5429)
    at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709)
    at android.app.Activity.startActivity(Activity.java:5927)
    at android.app.Activity.startActivity(Activity.java:5894)
    at com.example.savedtrial.MainActivity.onCreate$lambda$0(MainActivity.kt:23)
    at com.example.savedtrial.MainActivity.$r8$lambda$47nZOKobj58xqg2t-Q-x4Lw-Afk(Unknown Source:0)
    at com.example.savedtrial.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:4)
    at android.view.View.performClick(View.java:7506)
    at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
    at android.view.View.performClickInternal(View.java:7483)
    at android.view.View.-$$Nest$mperformClickInternal(Unknown Source:0)
    at android.view.View$PerformClick.run(View.java:29334)
    at android.os.Handler.handleCallback(Handler.java:942)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loopOnce(Looper.java:201)
    at android.os.Looper.loop(Looper.java:288)
    at android.app.ActivityThread.main(ActivityThread.java:7872)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
```

Here is my Mainfest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package = "com.example.savedtrial"
    xmlns:tools="http://schemas.android.com/tools">

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.SAVEDTRIAL"
        tools:targetApi="31">
        <activity
            android:name="com.example.savedtrial.contacting"
            android:exported="false"
            android:parentActivityName="com.example.savedtrial.MainActivity"/>
        <activity
            android:name="com.example.savedtrial.Collab"
            android:exported="false"
            android:parentActivityName="com.example.savedtrial.MainActivity"/>
        <activity
            android:name="com.example.savedtrial.notes"
            android:exported="false"
            android:parentActivityName="com.example.savedtrial.MainActivity" />
        <activity
            android:name="com.example.savedtrial.Calender"
            android:exported="false"
            android:parentActivityName="com.example.savedtrial.MainActivity" />
        <activity
            android:name="com.example.savedtrial.MainActivity"
            android:exported="true">

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>
    </application>

</manifest>

Main Kotlin file with the button code

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val actionBar = supportActionBar

        supportActionBar!!.setTitle("PROductivity")



        val calender: Button = findViewById(R.id.calender)

        calender.setOnClickListener {
            val intent = Intent(this, calender::class.java)
            startActivity(intent)
        }

        val notes: Button = findViewById(R.id.notes)

        notes.setOnClickListener {
            val intent = Intent(this, notes::class.java)
            startActivity(intent)
        }

        val contacting: Button = findViewById(R.id.contacting)

        contacting.setOnClickListener {
            val intent = Intent(this, contacting::class.java)
            startActivity(intent)
    }
        val collab: Button = findViewById(R.id.collab)

        collab.setOnClickListener {
            val intent = Intent(this, collab::class.java)
            startActivity(intent) }
    }

}

Here is the code for one of the files that is supposed to show up when the its respective button is clicked (Just an action bar with back button)

package com.example.savedtrial

import android.content.Intent
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class contacting : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_contacting)

        val actionBar = supportActionBar

        supportActionBar!!.setTitle("Contacting")

        supportActionBar!!.setDisplayHomeAsUpEnabled(true)
        }
}

My main activity layout

<?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">

    <Button
        android:id="@+id/calender"
        android:layout_width="126dp"
        android:layout_height="53dp"
        android:text="Calender"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.021"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.3" />

    <Button
        android:id="@+id/notes"
        android:layout_width="127dp"
        android:layout_height="54dp"
        android:text="Notes"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.021"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.473" />

    <Button
        android:id="@+id/contacting"
        android:layout_width="129dp"
        android:layout_height="51dp"
        android:text="Contacting"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.02"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.672" />

    <Button
        android:id="@+id/collab"
        android:layout_width="129dp"
        android:layout_height="51dp"
        android:text="Collab. Whiteboard"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.019"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.844" />

    <TextView
        android:id="@+id/productivity"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="PROductivity"
        android:textColor="#EA1616"
        android:textSize="34sp"
        android:textStyle="bold|italic"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

I am new to Android Studio, so if possible, try not to delve into extremely complicated stuff, but don't worry if you have to - I'll figure it out.


Solution

  • The important part of the error message is this part:

    Unable to find explicit activity class {com.example.savedtrial/com.google.android.material.button.MaterialButton}

    This means you passed a Button, not an Activity when you called startActivity. And indeed we can see this in your first setOnClickListener, where you refer to the wrong classes when constructing your intents:

    val intent = Intent(this, calender::class.java)
    

    You're using the Button instance named calendar for your class. It should be Calendar to match your manifest class - note the capital C to refer to your activity class, not your local variable:

    val intent = Intent(this, Calender::class.java)
    

    Similarly for every other click listener (except for maybe the notes activity, which you've also made lowercase, making it even harder to distinguish between a local variable and class name - you should always name your classes with a capital letter).

    You'll find things much easier to understand if you separate the names of your variables from your class names. That's why you usually see classes named like CalendarActivity, CollabActivity, etc. where you can more easily determine what is an activity instance versus what is a button that starts that activity (calendar, collab, etc.).