您需要在此活动中使用 Theme.AppCompat 主题(或后代)



Android Studio 0.4.5

用于创建自定义对话框的 Android 文档:http://developer.android.com/guide/topics/ui/dialogs.html

如果需要自定义对话框,则可以改为将活动显示为对话框,而不是使用对话框 API。只需创建一个活动并将其主题设置为 Theme.Holo.Dialog in<activity>清单元素:

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

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

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

您遇到此问题的原因是,您尝试将对话框主题应用到的活动正在扩展ActionBarActivity这需要应用AppCompat主题。

更新:扩展AppCompatActivity也会出现此问题

在这种情况下,将 Java 继承从ActionBarActivity更改为Activity,并将对话框主题保留在清单中,即非Theme.AppCompat


一般规则是,如果您希望您的代码支持旧版本的 Android,它应该具有AppCompat主题,并且 java 代码应该扩展AppCompatActivity。如果您有不需要此支持的活动,例如您只关心Android的最新版本和功能,则可以对其应用任何主题,但java代码必须扩展普通的旧Activity


注意:当从AppCompatActivity(或子类,ActionBarActivity)更改为Activity时,还必须将带有"support"的各种调用更改为没有"support">相应调用。 因此,与其getSupportFragmentManager,不如调用getFragmentManager

您需要做的就是将android:theme="@style/Theme.AppCompat.Light"添加到AndroidManifest.xml文件中的应用程序标记中。

从上面的评论中复制@MarkKeen的答案,因为我遇到了同样的问题。

我在帖子顶部指出了错误,并在我添加警报对话框后发生。我在清单中有所有相关的样式信息。我的问题已通过更改警报生成器中的上下文引用得到解决 - 我更改了:

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

自:

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

而且没有更多的问题。

如果您使用的是应用程序上下文,如下所示:

final AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());

将其更改为如下所示的活动上下文:

final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

min SDK 为 10。ActionBar可从 API 级别 11 获得。因此,对于 10 个,您将使用支持库中的AppCompat,您需要使用Theme.AppCompat或相同的后代。

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

或者,如果您不希望操作栏位于顶部

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

更多信息 @

http://developer.android.com/guide/topics/ui/actionbar.html

编辑:

我可能误读了op帖子。

似乎 op 想要一个带有活动主题的对话框。因此,正如Bobbake4已经建议的那样,扩展Activity而不是ActionBarActivity

还可以在下面的链接中查看@对话框属性

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.4_r1/frameworks/base/core/res/res/values/themes.xml/

即使我的主题是AppCompat主题并且我的活动是AppCompatActivity(或Activity,正如其他人的答案所建议的那样),我也遇到了这个问题。所以我清理、重建并重新运行了这个项目。

(构建 -> 清洁项目;构建 -> 重建项目 ;运行 ->运行)

它可能看起来很愚蠢,但现在它工作得很好

只是希望它有帮助!

这就是为我修复它的原因:我没有在清单中指定主题,而是在onCreate中为每个扩展ActionBarActivity的活动定义了它:

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

这里MyAppThemeTheme.AppCompat的后代,用xml定义。请注意,必须在super.onCreatesetContentView之前设置主题。

转到您的样式并放置父样式

parent="Theme.AppCompat"

而不是

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

更改所需活动的主题。这对我有用:

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

就我而言,我的 res 目录中没有 values-v21 文件。然后我创建了它并添加了以下代码:

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

Just Do

new AlertDialog.Builder(this)

而不是

new AlertDialog.Builder(getApplicationContext())

我在三星设备上遇到了这样的崩溃,即使该活动确实使用了Theme.AppCompat。 根本原因与三星方面的奇怪优化有关:

- if one activity of your app has theme not inherited from Theme.AppCompat
- and it has also `android:launchMode="singleTask"`
- then all the activities that are launched from it will share the same Theme

我的解决方案只是删除android:launchMode="singleTask"

对我来说,在尝试了所有解决方案之后,一个解决方案是改变

<activity
android:name="com.github.cythara.MainActivity"
android:label="Main">
</activity>

要包含主题:

<activity
android:name="com.github.cythara.MainActivity"
android:theme="@style/Theme.AppCompat.NoActionBar"
android:label="Main">
</activity>

如果您需要扩展 ActionBarActivity 您需要的样式.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:您需要在此活动中使用Theme.AppCompat主题(或后代)"错误。

我遇到了同样的问题,但是当我把它放在清单上时,它解决了:android:theme="@style/Theme.AppCompat。

<application
android:allowBackup="true"
android:icon="@drawable/icon"
android:label="@string/app_name_test"
android:supportsRtl="true"
android:theme="@style/Theme.AppCompat">
...    
</application>

就我而言,当我尝试显示对话框时出现了这样的问题。 问题出在上下文中,我使用了getBaseContext(),理论上它应该返回活动上下文,但似乎不是,或者它在应用任何主题之前返回上下文。

所以我只是用">this"替换了getBaseContexts(),现在它按预期工作。

Dialog.showAlert(this, title, message,....);

您之所以这样做,是因为您想在以前的 sdk 版本中将主题样式中的材质设计应用于 21。ActionBarActivity需要AppTheme因此,如果您还想阻止自己自定义 AppTheme,只需更改样式.xml(在 SDK 21 之前),因此可以继承 App Compat 主题。

<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar">

为此:

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

我有一个主题<android:theme="@android:style/Theme.Dialog">的活动,用于在我的appWidget中显示对话框,但我遇到了同样的问题

我通过更改活动代码解决了此错误,如下所示:

@Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.Theme_AppCompat_Dialog); //this line i added
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dialog);
}

请确保在创建新的警报对话框时使用活动上下文,而不是应用程序或基本上下文。

对我来说是使用ContextThemeWrapper的解决方案:

private FloatingActionButton getFAB() {
Context context = new android.support.v7.view.ContextThemeWrapper(getContext(), R.style.AppTheme);
FloatingActionButton fab = new FloatingActionButton(context);
return fab;}

来自安卓 - 如何以编程方式创建 FAB?

我也遇到了这个问题,我做了什么来修复它,并且仍然使用 Holo 主题是采取以下步骤:

首先我替换了这个导入:

import android.support.v7.app.AppCompatActivity;

有了这个:

import android.app.Activity;

然后将我的扩展名从:

public class MyClass extends AppCompatActivity {//...

对此:

public class MyClass extends Activity {//...

并且还必须更改此导入:

import android.support.v7.app.AlertDialog;

到此导入:

import android.app.AlertDialog;

然后,您可以在活动级别的清单中使用主题标签:

android:theme="@android:style/Theme.Holo.Dialog" />

最后,(除非您的项目中有其他必须使用 v7 appCompat 的类),您可以清理并重新构建您的项目,或者在应用程序级别删除 gradle 构建文件中的此条目:

compile 'com.android.support:appcompat-v7:23.2.1'

如果您的项目中有其他类必须使用 v7 appCompat,则只需清理并重新生成项目即可。

万一 AndroidXSplashScreen库把你带到这里......

这是因为Theme.SplashScreen也没有R.styleable.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.xmlTheme.SplashScreen就不会干扰。


也非常相关:使用Theme.MaterialComponents时,包含一个桥接主题,可以代替Theme.AppCompatTheme.MaterialComponents.DayNight.NoActionBar.Bridge

这个Bridge主题尽管Theme.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>

对于该错误,您有很多解决方案。

  1. 您应该使用ActivityFragmentActivity而不是ActionbarActivityAppCompatActivity

  2. 如果要使用ActionbarActivity或 AppCompatActivity,则应更改样式.xmlTheme.Holo.xxxxTheme.AppCompat.Light(如有必要,请添加到DarkActionbar)

如果您不需要有关操作栏或 AppCompat 的高级属性,则无需使用操作栏AppCompat

在 Android 清单中,只需将活动主题更改为 AppTheme 如下代码片段

<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme">
</activity>

根据我的经验,问题在于我显示对话的上下文。 在按钮内单击,我以这种方式实例化警报对话框:

builder = new AlertDialog.Builder(getApplicationContext());

但是上下文不正确并导致了错误。我以这种方式使用应用程序上下文对其进行了更改:

在声明部分中:

Context mContext;

在 onCreate 方法中

mContext = this;

最后在我需要警报对话框的代码中:

start_stop = (Button) findViewById(R.id.start_stop);
start_stop.setOnClickListener( new View.OnClickListener()
{
@Override
public void onClick(View v)
{
if (!is_running)
{
builder = new AlertDialog.Builder(mContext);
builder.setMessage("MYTEXT")
.setCancelable(false)
.setPositiveButton("SI", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Task_Started = false;
startTask();
}
})
.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}

这是我的解决方案。

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

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

一旦我将上面的主题添加到我的清单中,它就解决了问题。

我遇到了同样的问题。

如果要为任何类或方法提供上下文,请提供 YourActivityName.this 而不是 getApplicationContext()。

这样做

builder = new AlertDialog.Builder(YourActivity.this);

而不是

builder = new AlertDialog.Builder(getApplicationContext());

将主题样式父级更改为

parent="Theme.AppCompat"

这对我有用...

这个对我有用:

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

您的活动正在扩展ActionBarActivity,这需要应用AppCompat.theme。 从ActionBarActivity更改为ActivityFragmentActivity,它将解决问题。

如果您不使用操作栏,则:

android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" 

相关内容

最新更新