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>

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

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


当前回答

在我的情况下,我没有值v21文件在我的res目录。然后我创建了它,并添加了以下代码:

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

其他回答

这真的迫使我张贴我自己的答案。

因为我正在使用Theme.AppCompat.Light。NoActionBar,也用支持兼容性的导入替换了所有AlertDialog实例,但在v21 (Lollipop)下仍然面临问题。

我不喜欢改变主题的想法,这是合适的。 所以在2天之后,我终于想到了其他为AndroidManifest.xml指定AppTheme的库。

我发现还有另外一个:Paytm Checkout SDK。

因此,下面的更改解决了这个问题。

Renaming AppTheme using for my app to 'XYZAppTheme' using tools:replace method in AndroidManifest of my project(app). <uses-permission android:name="android.permission.INTERNET" /> <application android:name=".XYZ" android:allowBackup="true" android:icon="@mipmap/ic_launcher_v2" android:label="@string/app_name" android:largeHeap="true" android:roundIcon="@mipmap/ic_launcher_v2_round" android:supportsRtl="true" android:theme="@style/XYZAppTheme" tools:replace="android:icon,android:theme"> <activity android:name=".xyz.SplashActivity" android:launchMode="singleTask" android:screenOrientation="portrait"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>

而不是所有这些,在你的应用程序中改变很多。就像这个视频做的那样简单。

https://www.youtube.com/watch?v=Bsm-BlXo2SI

我已经用过了,所以…这是100%有效的。

你遇到这个问题的原因是因为你试图应用对话框主题的活动正在扩展ActionBarActivity,这需要应用AppCompat主题。

更新:扩展AppCompatActivity也会有这个问题

在本例中,将Java继承从ActionBarActivity更改为Activity,并在清单中保留对话框主题,即非theme。AppCompat价值


一般的规则是,如果你想让你的代码支持旧版本的Android,它应该有AppCompat主题,java代码应该扩展AppCompat活动。如果你有一个活动不需要这种支持,比如你只关心Android的最新版本和功能,你可以应用任何主题,但java代码必须扩展普通的旧活动。


注意:当从AppCompatActivity(或子类ActionBarActivity)更改为Activity时,也必须将各种带有“support”的调用更改为相应的不带“support”的调用。因此,调用getFragmentManager而不是getSupportFragmentManager。

如果你需要扩展ActionBarActivity,你需要在style.xml上:

<!-- Base application theme. -->
<style name="AppTheme" parent="AppTheme.Base"/>

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

如果你将应用程序的主题设置为android: theme . material。Light代替AppTheme。Base,那么你会得到一个“IllegalStateException:你需要使用一个主题。”AppCompat主题(或后代)与此活动“错误。

当我试图将这个免费的资源管理器模块(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文件中删除它)。 出于某种原因,现代版本有了细微的变化。这是显而易见的,但如果某样东西起作用了,那么它通常是正确的方式……