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>

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

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


当前回答

注意:我本来打算用这个作为答案,但进一步的测试显示,从命令行使用maven构建时仍然会失败,所以我不得不对它进行编辑,使之成为一个问题!: - (

在我的情况下,当我得到这个错误时,我已经在使用AppCompat主题和错误没有多大意义。

我正在构思我的安卓系统。我已经依赖于apklib和jar版本的应用程序compat,因此:

    <!-- See https://github.com/mosabua/maven-android-sdk-deployer -->
        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>${compatibility.version}</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7</artifactId>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v4</artifactId>
        </dependency>

现在,当我导入maven项目并从IntelliJ构建和运行时,一切都好了。

但是当我使用maven从命令行构建、部署和运行时,我仍然会得到这个异常。

其他回答

对于我的Xamarin Android项目(在MainActivity.cs中),我改变了…

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity

to

public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsApplicationActivity

然后错误就消失了。我知道这不是每个人的解决方案,但它可能会给潜在问题提供线索。

转到你的样式,把父元素

parent="Theme.AppCompat"

而不是

parent="@android:style/Theme.Holo.Light"

当我试图将这个免费的资源管理器模块(NoNonsense - FilePicker)添加到我当前的应用程序时,我得到了这个异常:

illegalstateexception:您需要使用主题。AppCompat主题(或后代)

为使模块工作而添加的代码是在之前的App中的styles.xml文件中,并且它工作得很好:

<!-- You can also inherit from NNF_BaseTheme.Light -->
<style name="FilePickerTheme" parent="NNF_BaseTheme">
    <!-- Set these to match your theme -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>

    <!-- Setting a divider is entirely optional -->
    <item name="nnf_list_item_divider">?android:attr/listDivider</item>

    <!-- Need to set this also to style create folder dialog -->
    <item name="alertDialogTheme">@style/FilePickerAlertDialogTheme</item>

    <!-- If you want to set a specific toolbar theme, do it here -->
    <!-- <item name="nnf_toolbarTheme">@style/ThemeOverlay.AppCompat.Dark.ActionBar</item> -->
</style>

<style name="FilePickerAlertDialogTheme" parent="Theme.AppCompat.Dialog.Alert">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

但这在最近的应用程序中不起作用。解决方案很简单,我只是将代码移动到themes.xml文件(并从styles.xml文件中删除它)。 出于某种原因,现代版本有了细微的变化。这是显而易见的,但如果某样东西起作用了,那么它通常是正确的方式……

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

注意:我本来打算用这个作为答案,但进一步的测试显示,从命令行使用maven构建时仍然会失败,所以我不得不对它进行编辑,使之成为一个问题!: - (

在我的情况下,当我得到这个错误时,我已经在使用AppCompat主题和错误没有多大意义。

我正在构思我的安卓系统。我已经依赖于apklib和jar版本的应用程序compat,因此:

    <!-- See https://github.com/mosabua/maven-android-sdk-deployer -->
        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <version>${compatibility.version}</version>
            <type>apklib</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7-appcompat</artifactId>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v7</artifactId>
            <type>jar</type>
        </dependency>

        <dependency>
            <groupId>android.support</groupId>
            <artifactId>compatibility-v4</artifactId>
        </dependency>

现在,当我导入maven项目并从IntelliJ构建和运行时,一切都好了。

但是当我使用maven从命令行构建、部署和运行时,我仍然会得到这个异常。