Unable to find explicit activity class. Have you d

2020-07-14 05:30发布

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);
    }

8条回答
SAY GOODBYE
2楼-- · 2020-07-14 05:47

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");
查看更多
Bombasti
3楼-- · 2020-07-14 05:52

Change this

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

to

<activity android:name=".AppPreferenceActivity"
        android:label="Preferences">
查看更多
Bombasti
4楼-- · 2020-07-14 05:55

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

查看更多
【Aperson】
5楼-- · 2020-07-14 06:03

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);
    }
查看更多
疯言疯语
6楼-- · 2020-07-14 06:07

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

查看更多
看我几分像从前
7楼-- · 2020-07-14 06:07

Change

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

to

<activity android:name="com.iphonik.AppPreferenceActivity"
        android:label="Preferences">
查看更多
登录 后发表回答