2

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?

user616076
  • 3,907
  • 8
  • 38
  • 64

3 Answers3

0

Declare your SettingsActivity in your android manifest:

<activity
     android:name=".SettingsActivity">
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
    </intent-filter>
</activity>
xdevs23
  • 3,824
  • 3
  • 20
  • 33
0

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" />
Sohail Zahid
  • 8,099
  • 2
  • 25
  • 41
0

Check Manifest file. The <activity> tag for .MainActivity is not closed.

Your Manifest file should be looks like this.

    <?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>// add this
        <activity android:name=".SettingsActivity" />

Hope it helps!!

venkatesh gowda
  • 841
  • 2
  • 12
  • 26