android 为什么会出现“无法找到显式活动类”错误?

jm2pwxwz  于 2023-03-16  发布在  Android
关注(0)|答案(1)|浏览(137)

I changed application icon if user is gold user. When if the user is not gold after I changed that, don't load splash activity again. I tried create two alias actvity for these two sitatuan but I can't change icon with two alias. I tried deleted <action android:name="android.intent.action.MAIN"/> in SplashActivity but didn't solve. When I use these code without call from HomeFragment. Why have this issue now?
HomeFragmet.kt

if (clientPreferences.getIconType() != moneyCardType){
                                clientPreferences.setIsMoneyGold(moneyCardType)
                                clientPreferences.setIconType(moneyCardType)
                                val i = Intent(requireContext(), SplashActivity::class.java)
                                startActivity(i)
                            }

SplashActivity.kt

if (clientPreferences.getIconType()){
            // disable old icon
            val manager = packageManager
            manager.setComponentEnabledSetting(
                ComponentName(
                    this@SplashActivity,
                    "com.grosmagazam.ui.SplashActivity"
                ),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )

            // enable new icon
            manager.setComponentEnabledSetting(
                ComponentName(
                    this@SplashActivity,
                    "com.grosmagazam.ui.SplashActivityAlias"
                ),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )
        } else {
            // enable old icon
            val manager = packageManager
            manager.setComponentEnabledSetting(
                ComponentName(
                    this@SplashActivity,
                    "com.grosmagazam.ui.SplashActivity"
                ),
                PackageManager.COMPONENT_ENABLED_STATE_ENABLED,
                PackageManager.DONT_KILL_APP
            )
            // disable new icon
            manager.setComponentEnabledSetting(
                ComponentName(
                    this@SplashActivity,
                    "com.grosmagazam.ui.SplashActivityAlias"
                ),
                PackageManager.COMPONENT_ENABLED_STATE_DISABLED,
                PackageManager.DONT_KILL_APP
            )
        }

AndroidManifest.xml

<activity
        android:name=".ui.SplashActivity"
        android:exported="true"
        android:enabled="true"
        android:screenOrientation="locked"
        android:theme="@style/Theme.Gros">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

    <activity-alias
        android:name=".ui.SplashActivityAlias"
        android:roundIcon="@drawable/ic_gros_money_gold_app"
        android:icon="@drawable/ic_gros_money_gold_app"
        android:enabled="false"
        android:exported="true"
        android:screenOrientation="locked"
        android:theme="@style/Theme.Gros"
        android:targetActivity=".ui.SplashActivity">

        <intent-filter>

            <action android:name="android.intent.action.MAIN"/>

            <category android:name="android.intent.category.LAUNCHER" />

        </intent-filter>

    </activity-alias>

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.grosmagazam/com.grosmagazam.ui.SplashActivity}; have you declared this activity in your AndroidManifest.xml, or does your intent not match its declared ? at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2171) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1805) at android.app.Activity.startActivityForResult(Activity.java:5583) at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2171) at android.app.Instrumentation.execStartActivity(Instrumentation.java:1805) at android.app.Activity.startActivityForResult(Activity.java:5583) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:728) at android.app.Activity.startActivityForResult(Activity.java:5541) at androidx.activity.ComponentActivity.startActivityForResult(ComponentActivity.java:709) at android.app.Activity.startActivity(Activity.java:6039) at androidx.core.content.ContextCompat$Api16Impl.startActivity(ContextCompat.java:978) at androidx.core.content.ContextCompat.startActivity(ContextCompat.java:318) at androidx.fragment.app.FragmentHostCallback.onStartActivityFromFragment(FragmentHostCallback.java:167) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1445) at androidx.fragment.app.Fragment.startActivity(Fragment.java:1433) at com.grosmagazam.ui.home.HomeFragment$initObservers$1.invoke(HomeFragment.kt:119) at com.grosmagazam.ui.home.HomeFragment$initObservers$1.invoke(HomeFragment.kt:70) at com.grosmagazam.ui.home.HomeFragment.initObservers$lambda$2(HomeFragment.kt:70) at com.grosmagazam.ui.home.HomeFragment.$r8$lambda$hkW920MYp10V-EdGJmRsINNTQI4(Unknown Source:0) at com.grosmagazam.ui.home.HomeFragment$$ExternalSyntheticLambda1.onChanged(Unknown Source:2)

gcuhipw9

gcuhipw91#

Intent(requireContext(), SplashActivity::class.java)
禁用此Activity时,就像清单中不存在它一样。您需要改为启动别名。
这不可能通过类文本实现,但可以将其名称指定为字符串like this

相关问题