Android Studio 0.4.5

用于创建自定义对话框的Android文档:http://developer.android.com/guide/topics/ui/dialogs.html

如果您想要一个自定义对话框,您可以将活动显示为对话框,而不是使用对话框api。简单地创建一个活动,并将其主题设置为theme . holo . dialog in <activity> manifest元素:

<activity android:theme="@android:style/Theme.Holo.Dialog" >

然而,当我尝试这样做时,我得到以下异常:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

我支持以下,我不能使用大于10的最小值:

minSdkVersion 10
targetSdkVersion 19

在我的风格中,我有以下几点:

<!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

在我的清单上,我有这样的活动:

 <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:theme="@android:style/Theme.Holo.Light.Dialog"
            android:name="com.ssd.register.Dialog_update"
            android:label="@string/title_activity_dialog_update" >
        </activity>

创建这样的对话框是我一直在做的事情,因为我已经完成了布局。

谁能告诉我如何解决这个问题?


当前回答

在Android manifest中,只需将活动主题更改为AppTheme,如下代码片段所示

<activity
  android:name=".MainActivity"
  android:label="@string/app_name"
  android:theme="@style/AppTheme">
</activity>

其他回答

所有你需要做的是添加android:theme="@style/ theme . appcompat。在AndroidManifest.xml文件中将“Light”添加到应用程序标签中。

我有一个活动的主题<android:主题="@android:风格/主题。对话框“>用于显示对话框在我的appWidget和我有同样的问题

我通过更改活动代码解决了这个错误,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_dialog);
}

在MaterialAlertDialogBuilder中使用“this”而不使用“applicationContext”

MaterialAlertDialogBuilder(this).show()

在我的例子中,我用ApplicationContext来膨胀一个视图。当你使用ApplicationContext时,theme/style不会被应用,所以尽管有theme . context。Appcompat在我的风格,它没有应用,导致这个异常。更多细节:当膨胀器与ApplicationContext一起使用时,主题/样式不应用

我在使用SplashScreen Api时遇到了这个问题,当MainActivity中没有调用installSplashScreen()时,也会抛出这个错误

override fun onCreate(savedInstanceState: Bundle?) {
        installSplashScreen()
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
}