-4

I just built a very simple app and tried to test it. The app just contains a button (for now), clicking which opens another activity. Here's the error I get:

Unable to find explicit activity class {com.example.mathshelper/com.example.mathshelper.peri_area_rect}; have you declared this activity in your AndroidManifest.xml?

Here's my androidmanifest.xml:

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

How to fix it?

Edit: here's the activity class code:

package com.example.mathshelper

import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem

import kotlinx.android.synthetic.main.activity_main.*
import kotlinx.android.synthetic.main.content_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        setSupportActionBar(toolbar)

        goToPeriAreRctangle.setOnClickListener {
            startActivity(Intent(this, peri_area_rect::class.java))
        }

    }
}

EDIT: I am very sorry guys, I was following a wrong approach to add a new activity, basically, I should have gone to File > New > Activity.

DeathVenom
  • 384
  • 3
  • 22

2 Answers2

1

You have to create activity before you can use it.

To create a new activity go to

File -> New -> Activity and select empty activity

Now This will open a separate window. Here give the activity name that you want and click finish, This will create the activity for you. Use that same name to start the activity on button click

karan
  • 8,637
  • 3
  • 41
  • 78
1

Define your second activity in AndroidManifest.xml file like below...

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

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name=".peri_area_rect"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"/>
    </application>

</manifest>
Jaimil Patel
  • 1,301
  • 6
  • 13