如何在没有 public 构造函数的情况下通过 Android 清单?

How to get past Android manifest without a public constructor?

在我的清单中我有这个:

<activity
  android:name=".BackgroundOptionSlider"
  android:label="@string/update_background" >
</activity>

但是我的 class、BackgroundOptionsSlider 需要为我正在做的事情提供一个私有的默认构造函数(确保只有一个实例),因此我收到一个错误在我的清单中说明我需要一个 public 默认构造函数。我怎样才能克服这个问题?

由于您的 BackgroundOptionSlider 扩展了 Activity,您无法避免使用默认的 public 构造函数。

making sure there is only one instance ever

系统已经给你实现的方法,声明

<activity
        android:name=".BackgroundOptionSlider"
        android:label="@string/update_background"
        android:launchMode="singleTask">
    </activity>

<activity
        android:name=".BackgroundOptionSlider"
        android:label="@string/update_background"
        android:launchMode="singleInstance">
    </activity>

More information about the launch mode, and also refer to this question.

你永远不应该尝试自己实例化 Activity android 框架会为你做到这一点,所以你应该提供一个默认的 public 构造函数,如果你只想有一个activity 的实例,您可以通过将启动模式指定为 singleInstance 来实现,here 这里有一篇关于 launchModes

的好书