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>

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

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


当前回答

把它做成

.getTheme getApplicationContext () () .applyStyle (R.style.Theme_Mc, 真正的);

在values/styles.xml中 添加这个

 <style  name="Theme.Mc" parent="Theme.AppCompat.Light.NoActionBar">
         <!-- ADD Your Styles  -->


     </style>

其他回答

在我的情况下,这样的问题出现时,我试图显示对话。 问题是在上下文中,我使用了getBaseContext(),理论上应该返回活动上下文,但似乎不是,或者它在任何主题应用之前返回上下文。

所以我只是用“this”替换了getBaseContexts(),现在它像预期的那样工作了。

        Dialog.showAlert(this, title, message,....);

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

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

这个对我很有用:

<application
           android:allowBackup="true"
           android:icon="@mipmap/ic_launcher"
           android:label="@string/app_name"
           android:theme="@style/AppTheme" >
           <activity
               android:name=".MainActivity"
               android:label="@string/app_name"
               android:theme="@style/Theme.AppCompat.NoActionBar">

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

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

对我来说,即使在我拥有了《Theme》之后,上述答案也都不适用。AppCompat作为应用程序的基本主题。我用的是com.android.support:design:24.1.0,所以我把版本改成了24.1.1。gradle同步后,它再次工作,异常消失了。在我看来,问题出在一些老版本的设计支持库上。

把这个放在这里,以防其他答案对某些人不适用。

对我来说,通过在我的customDialog类中更改从AppCompatActivity到Activity的继承来解决问题。无需更改Theme.Dialog的manifest。