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>

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

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


当前回答

最小SDK为10。ActionBar从api级别11可用。因此,对于10,您将使用来自支持库的AppCompat,您需要使用Theme。AppCompat或其后代。

Use

android:theme="@style/Theme.AppCompat" >

或者如果你不想在顶部的操作栏

android:theme="@style/Theme.AppCompat.NoActionBar">

更多信息@

http://developer.android.com/guide/topics/ui/actionbar.html

编辑:

我可能看错了行动报告。

似乎op想要一个带有活动主题的对话框。正如Bobbake4所建议的,扩展Activity而不是ActionBarActivity。

也可以看看下面链接中的@ Dialog Attributes

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/

其他回答

快速的解决方案。

在styles.xml中更改基本主题父元素

取代的

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

to

<style name="AppTheme" parent="Theme.AppCompat">

我也遇到了同样的问题。因为我正在创建自定义导航抽屉。但我忘记在清单上提到主题了

android:theme="@style/Theme.AppCompat.NoActionBar"

当我把上面的主题添加到我的清单时,问题就解决了。

如果您正在与Recyclerview Adapter类斗争,那么使用

view.getRootView().getContext()

而不是

getApplicationContext() or activity.this

确保在创建新的Alert Dialog时使用的是活动上下文,而不是应用程序或基本上下文。

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

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