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>

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

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


当前回答

我被这个问题难住了好几个小时。问题是我实现了自己的.jar依赖项,没有使用Theme。主题。xml和AndroidManifest.xml中的AppCompat样式。即使在我改变和重建我的依赖之后,它也没有起作用。经过一些研究,我发现我应该使用这里描述的.aar (Android Archive)库。在将我的依赖模块更改为Android库后,我可以毫无错误地构建和实现它。希望这能有所帮助。

其他回答

复制上面评论中@MarkKeen的答案,因为我也有同样的问题。

我在帖子的顶部声明了错误,并在我添加警告对话框后发生。我在清单中有所有相关的样式信息。我的问题是通过更改警报构建器中的上下文引用来解决的-我更改了:

new android.support.v7.app.AlertDialog.Builder(getApplicationContext())

to:

new android.support.v7.app.AlertDialog.Builder(this)

不会再有问题了。

首先,将其添加为import => import androidx.appcompat.app.AlertDialog

我发布这个是有史以来最小的问题解决方案。 我只是改变了实例化

new AlertDialog.Builder(mContex)

to

new AlertDialog。Builder (mContext R.style.PreferenceDialogLight)

Where <style name="PreferenceDialogLight" parent="Base.Theme.MaterialComponents.Dialog.Alert">

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

当你使用Kotlin时,没有什么能解决我的问题,直到我将生成器参数从“applicationContext”更改为“this”。

这行不通

val dialogDelete = AlertDialog.Builder(applicationContext)
            .setTitle("Confirmation")
            .setMessage("Delete this photo?")
            .setNegativeButton(android.R.string.no){ it, which ->
                it.dismiss()
            }
dialogDelete.show()

遵循代码工作

val dialogDelete = AlertDialog.Builder(this)
            .setTitle("Confirmation")
            .setMessage("Delete this photo?")
            .setNegativeButton(android.R.string.no){ it, which ->
                it.dismiss()
            }
dialogDelete.show()

更改所需活动的主题。这对我来说很管用:

<activity
            android:name="HomeActivity"
            android:screenOrientation="landscape"
            android:theme="@style/Theme.AppCompat.Light"
            android:windowSoftInputMode="stateHidden" />