首页 > 解决方案 > 我想更改启动器 Activity 而不会崩溃我的应用程序并在主屏幕上保留我的应用程序的快捷方式

问题描述

这是一个简单的项目。我正在使用 2 个活动。

  1. 主要活动
  2. 新活动

我为 MainActivity 做了 3 个别名

我想根据我制作的活动别名更改启动器的图标和标签。为此,我在新活动中制作了 3 个按钮。MainActivity 只是调用 NewActivity。但该应用程序只是崩溃生成错误

这是 manifest.xml:

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

    <application
        android:allowBackup="true"
        android:enabled="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">

        <activity
            android:name=".NewActivity"
            android:label="@string/app_name" />

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity-alias
            android:name=".RED"
            android:enabled="true"
            android:icon="@drawable/red"
            android:label="Red app"
            android:targetActivity=".MainActivity">

        </activity-alias>

        <activity-alias
            android:name=".BLUE"
            android:enabled="false"
            android:icon="@drawable/blue"
            android:label="Blue app"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>

        <activity-alias
            android:name=".GREEN"
            android:enabled="false"
            android:icon="@drawable/green"
            android:label="Green app"
            android:targetActivity=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity-alias>


    </application>

</manifest>

这是 MainActivity.java:

package com.example.app_alias_check;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

class MainActivity extends AppCompatActivity {
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState, @Nullable PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        Intent intent = new Intent(this, NewActivity.class);
        startActivity(intent);
    }
}

这是NewActivity.java:

package com.example.app_alias_check;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ComponentName;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class NewActivity extends AppCompatActivity implements View.OnClickListener {

     private String []iconColour = {"RED", "GREEN",  "BLUE"};
    private Button bRed, bGreen, bBlue;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new);
        bRed = findViewById(R.id.btn_red);
        bGreen = findViewById(R.id.btn_green);
        bBlue = findViewById(R.id.btn_blue);
        bRed.setOnClickListener(this);
        bGreen.setOnClickListener(this);
        bBlue.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn_red:
                setIcon("Red");
                break;
            case R.id.btn_green:
                setIcon("Green");
                break;
            case R.id.btn_blue:
                setIcon("Blue");
                break;
        }
    }

    private void setIcon(String targetColour) {
        int action;
        for (String value : iconColour) {
            if (value.equals(targetColour))
                action = PackageManager.COMPONENT_ENABLED_STATE_ENABLED;
            else
                action = PackageManager.COMPONENT_ENABLED_STATE_DISABLED;

            ComponentName componentName = new ComponentName(BuildConfig.APPLICATION_ID, BuildConfig.APPLICATION_ID + value);
            getPackageManager().setComponentEnabledSetting(
                    componentName,
                    action, PackageManager.DONT_KILL_APP);
        }
    }


}

这是activity_new.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewActivity">

    <Button
        android:id="@+id/btn_red"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Red"
        app:layout_constraintBottom_toTopOf="@+id/btn_green"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_blue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Blue"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_green" />

    <Button
        android:id="@+id/btn_green"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Green"
        app:layout_constraintBottom_toTopOf="@+id/btn_blue"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.501"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btn_red" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是错误日志:

    --------- beginning of crash
07-17 20:06:49.420 3047-3047/com.example.app_alias_check E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.app_alias_check, PID: 3047
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.app_alias_check/com.example.app_alias_check.MainActivity}: java.lang.IllegalAccessException: class com.example.app_alias_check.MainActivity is not accessible from class android.app.Instrumentation
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
        at android.app.ActivityThread.access$800(ActivityThread.java:144)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.IllegalAccessException: class com.example.app_alias_check.MainActivity is not accessible from class android.app.Instrumentation
        at java.lang.Class.newInstance(Class.java:1557)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
        at android.app.ActivityThread.access$800(ActivityThread.java:144) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:135) 
        at android.app.ActivityThread.main(ActivityThread.java:5221) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:372) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

我不知道是否允许为一个活动创建多个别名或只允许一个别名?

标签: androidandroid-activityandroid-activity-alias

解决方案


<activity-alias>允许多个。

您需要声明MainActivity为一个public类。将public关键字添加到

class MainActivity extends AppCompatActivity {

推荐阅读