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>

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

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


当前回答

如果AndroidX SplashScreen库把你带到这里…

这是因为Theme。SplashScreen也没有r.s eleable . appcompattheme_windowactionbar:

if (!a.hasValue(R.styleable.AppCompatTheme_windowActionBar)) {
    a.recycle();
    throw new IllegalStateException(
            "You need to use a Theme.AppCompat theme (or descendant) with this activity.");
}

这需要在调用super()之前将主题切换到postSplashScreenTheme:

@Override
protected void onCreate(Bundle savedInstanceState) {

    /* When switching the theme to dark mode. */
    if (savedInstanceState != null) {
        this.setTheme(R.style.AppTheme);
    }
    super.onCreate(savedInstanceState);
    
    /* When starting the Activity. */
    if (savedInstanceState == null) {
        SplashScreen.installSplashScreen(this);
    }
}

然后是主题。来自AndroidManifest.xml的SplashScreen不会干扰。


同样相关的还有:当使用Theme时。MaterialComponents,有一个桥主题包含,它作为主题的替代品。AppCompat: Theme.MaterialComponents.DayNight.NoActionBar.Bridge。

这个桥的主题工作尽管主题。MaterialComponents不从Theme继承。AppCompat:

<?xml version="1.0" encoding="utf-8"?>
<resources>    
    <style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge" />   
    <style name="AppTheme.SplashScreen" parent="Theme.SplashScreen" />    
</resources>

其他回答

注意:我本来打算用这个作为答案,但进一步的测试显示,从命令行使用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从命令行构建、部署和运行时,我仍然会得到这个异常。

这是什么固定它为我:而不是指定在manifest主题,我定义它在onCreate为每个扩展ActionBarActivity的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    setTheme(R.style.MyAppTheme);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.my_activity_layout);
...
}

这里MyAppTheme是Theme的后代。在xml中定义。注意,主题必须设置在super之前。onCreate和setContentView。

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

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

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

检查是否向Dialog传递了正确的上下文。这发生在我身上,然后我意识到我正在启动AlertDialog。使用applicationContext而不是Activity上下文的构建器。

这个解决方案对我很有效。

设计库依赖于Support v4和AppCompat支持库,所以不要在gradle中使用不同版本的AppCompat和设计库。

use

 compile 'com.android.support:appcompat-v7:23.0.0'
 compile 'com.android.support:design:23.0.0'

而不是

 compile 'com.android.support:appcompat-v7:23.0.0'
 compile 'com.android.support:design:23.1.0'