Unable to find explicit activity class. Have you d

2020-07-14 06:02发布

问题:

I'm trying to implement a PreferenceActivity in my app, following the accepted answer in this question

I get the above exception

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.iphonik.chameleon/com.iphonik.AppPreferenceActivity}; have you declared this activity in your AndroidManifest.xml?

AndroidManifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".MainMenu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Movies" />
        <activity android:name=".SettingsActivity" />
        <activity android:name=".InfoActivity" />

        <receiver
            android:name=".AppBroadcastReciever"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <activity android:name=".Info2Activity" />
        <activity android:name=".ItemDetailActivity" />
        <activity android:name=".TVActivity" />
        <activity android:name="com.iphonik.chameleon.AppPreferenceActivity"
            android:label="Preferences">
        </activity>
    </application>

</manifest>

The activity launching code

@Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.preferences:
                Intent intent = new Intent();
                intent.setClassName(this, "com.iphonik.AppPreferenceActivity");
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

回答1:

declare your activity like this:

<activity
        android:name=".AppPreferenceActivity"
        android:label="Preferences" >
        <intent-filter>
            <action android:name="com.iphonik.chameleon.AppPreferenceActivity" />
            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

and use it:

Intent i = new Intent("com.iphonik.chameleon.AppPreferenceActivity");


回答2:

From your manifest

<activity android:name="com.iphonik.chameleon.AppPreferenceActivity"
            android:label="Preferences">
        </activity>

remove com.iphonik.chameleon this

and looks like

<activity android:name=".AppPreferenceActivity"
                android:label="Preferences">
            </activity>


回答3:

Change this lines

<activity android:name="com.iphonik.chameleon.AppPreferenceActivity"
            android:label="Preferences">
        </activity>

to

<activity android:name="com.iphonik.AppPreferenceActivity"
            android:label="Preferences">
        </activity>

in Manifest



回答4:

Change

<activity android:name="com.iphonik.chameleon.AppPreferenceActivity" 
        android:label="Preferences">

to

<activity android:name="com.iphonik.AppPreferenceActivity"
        android:label="Preferences">


回答5:

you must checked that if this path is excist in your class path. com.iphonik.chameleon.AppPreferenceActivity.



回答6:

Change this

<activity android:name="com.iphonik.chameleon.AppPreferenceActivity"
        android:label="Preferences">

to

<activity android:name=".AppPreferenceActivity"
        android:label="Preferences">


回答7:

Use below code

Manifest.xml

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

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.GET_TASKS" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />
    <uses-permission android:name="android.permission.RECEIVE_BOOT_COMPLETED" />
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@android:style/Theme.NoTitleBar">
        <activity android:name=".MainMenu">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".Movies" />
        <activity android:name=".SettingsActivity" />
        <activity android:name=".InfoActivity" />

        <receiver
            android:name=".AppBroadcastReciever"
            android:enabled="true"
            android:exported="true"
            android:permission="android.permission.RECEIVE_BOOT_COMPLETED">
            <intent-filter>
                <action android:name="android.intent.action.BOOT_COMPLETED" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </receiver>

        <activity android:name=".Info2Activity" />
        <activity android:name=".ItemDetailActivity" />
        <activity android:name=".TVActivity" />
        <activity android:name=".AppPreferenceActivity"
            android:label="Preferences">
        </activity>
    </application>

</manifest>

The activity launching code

@Override
    public boolean onOptionsItemSelected(MenuItem item){
        switch(item.getItemId()) {
            case R.id.preferences:
                Intent intent = new Intent(currentActivityName.this, AppPreferenceActivity.class);
                startActivity(intent);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }


回答8:

use correct package name and activity name should be start with dot (.)

example:

<manifest package="your.package.name">
  <application>
    <activity android:name=".ActivityName"/>
    //if you have sub packages
    <activity android:name=".subPackage.ActivityName"/>
  </application>
</manifest>