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>

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

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


当前回答

对于这个错误,有很多解决方案。

你应该使用Activity或FragmentActivity而不是ActionbarActivity或AppCompatActivity 如果你想使用ActionbarActivity或AppCompatActivity,你应该把styles.xml Theme.Holo.xxxx改成Theme.AppCompat.Light(如果需要的话添加到DarkActionbar)

如果你不需要关于动作栏或AppCompat的高级属性,你不需要使用动作栏或AppCompat。

其他回答

我的活动与SectionsPagerAdapter和ViewPager & Fragment

public class MyActivity extends AppCompatActivity implements ActionBar.TabListener
...
...
     @Override
        public void onPostResume(){
            super.onPostResume();
            try {
               getSupportActionBar().setDisplayShowTitleEnabled(false);
            }catch (NullPointerException ignored){
            }
        }

如果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>

在MaterialAlertDialogBuilder中使用“this”而不使用“applicationContext”

MaterialAlertDialogBuilder(this).show()

在我的情况下,我没有值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>

我遇到这个问题,即使我的主题是一个AppCompat主题和我的活动是一个AppCompatActivity(或活动,建议对其他人的答案)。所以我清理、重建并重新运行这个项目。

(Build -> Clean Project;Build ->重建项目;Run -> Run)

这可能看起来很愚蠢,但现在它很有效!

希望这能有所帮助!