Search code examples
androidandroid-intentmanifeststart-activity

Error when trying to open new activity from menu


I'm trying to move to a new activity from a selected option from a menu on my main screen. The code for doing this is

public boolean onOptionsItemSelected(MenuItem item) {
    switch(item.getItemId()) {
        case R.id.action_settings:
            Intent settingsIntent = new Intent(this, SettingsActivity.class);
            this.startActivity(settingsIntent);
            break;

The activity is set up in my manifest file as below

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

<uses-permission android:name="android.permission.BLUETOOTH" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar"
        android:screenOrientation="landscape"
        android:configChanges="keyboardHidden|orientation">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
        <activity android:name=".SettingsActivity" />

The app crashes as soon as it hits the startActivity line with error

Unable to find explicit activity class {com.test.test_controller/com.test.test_controller.SettingsActivity}; have you declared this activity in your AndroidManifest.xml?

I've been away from Android for quite a while so I can't understand why is it putting the package name in twice? I've checked my settings and the App Id is the same as the package name in the manifest so what am I doing wrong?


Solution

  • Add this </activity> tag after <intent-filter> so system will consider them two activities.

    <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar"
            android:screenOrientation="landscape"
            android:configChanges="keyboardHidden|orientation">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            </activity> // add this tag here 
            <activity android:name=".SettingsActivity" />