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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21814825/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:08:04  来源:igfitidea点击:

You need to use a Theme.AppCompat theme (or descendant) with this activity

androidandroid-layout

提问by ant2009

Android Studio 0.4.5

安卓工作室 0.4.5

Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html

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

If you want a custom dialog, you can instead display an Activity as a dialog instead of using the Dialog APIs. Simply create an activity and set its theme to Theme.Holo.Dialog in the <activity>manifest element:

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

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

However, when I tried this I get the following exception:

但是,当我尝试此操作时,出现以下异常:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity

I am supporting the following, and I can't using something greater than 10 for the min:

我支持以下内容,并且我不能为 min 使用大于 10 的值:

minSdkVersion 10
targetSdkVersion 19

In my styles I have the following:

在我的风格中,我有以下几点:

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

And in my manifest I have this for the activity:

在我的清单中,我有这个活动:

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

Creating the dialog box like this was something I was hopping to do, as I have already completed the layout.

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

Can anyone tell me how I can get around this problem?

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

回答by Bobbake4

The reason you are having this problem is because the activity you are trying to apply the dialog theme to is extending ActionBarActivitywhich requires the AppCompattheme to be applied.

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

Update: Extending AppCompatActivitywould also have this problem

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

In this case, change the Java inheritance from ActionBarActivityto Activityand leave the dialog theme in the manifest as it is, a non Theme.AppCompatvalue

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



The general rule is that if you want your code to support older versions of Android, it should have the AppCompattheme and the java code should extend AppCompatActivity. If you have *an activity that doesn't need this support, such as you only care about the latest versions and features of Android, you can apply any theme to it but the java code must extend plain old Activity.

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



NOTE: When change from AppCompatActivity(or a subclass, ActionBarActivity), to Activity, must also change the various calls with "support" to the corresponding call without"support". So, instead of getSupportFragmentManager, call getFragmentManager.

注:从变化AppCompatActivity(或子类,ActionBarActivity),对Activity,也必须改变以“支持”到相应的调用各种调用“支持”。所以,而不是getSupportFragmentManager调用getFragmentManager

回答by iusting

All you need to do is add android:theme="@style/Theme.AppCompat.Light"to your application tag in the AndroidManifest.xmlfile.

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

回答by A.K.

Copying answer from @MarkKeen in the comments above as I had the same problem.

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

I had the error stated at the top of the post and happened after I added an alert dialog. I have all the relevant style information in the manifest. My problem was cured by changing a context reference in the alert builder - I changed:

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

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

to:

到:

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

And no more problems.

没有更多的问题。

回答by Raghunandan

min sdk is 10. ActionBaris available from api level 11. So for 10 you would be using AppCompatfrom the support library for which you need to use Theme.AppCompator descendant of the same.

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

Use

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

Or if you dont want action bar at the top

或者,如果您不想要顶部的操作栏

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

More info @

更多信息 @

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

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

Edit:

编辑:

I might have misread op post.

我可能误读了op post。

Seems op wants a Dialog with a Activity Theme. So as already suggested by Bobbake4 extend Activityinstead of ActionBarActivity.

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

Also have a look @ Dialog Attributes in the link below

还可以查看下面链接中的@Dialog Attributes

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

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

回答by Darush

If you are using the application context, like this:

如果您正在使用应用程序上下文,如下所示:

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

change it to an activity context like this:

将其更改为这样的活动上下文:

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

回答by Geraldo Neto

I was experiencing this problem even though my Theme was an AppCompatTheme and my Activity was an AppCompatActivity(or Activity, as suggested on other's answers). So I cleaned, rebuild and rerun the project.

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

(Build -> Clean Project ; Build -> Rebuild Project ; Run -> Run)

构建 -> 清理项目;构建 -> 重建项目;运行 -> 运行

It may seem dumb, but now it works great!

它可能看起来很愚蠢,但现在它很好用

Just hope it helps!

只是希望它有帮助!

回答by k29

This is what fixed it for me: instead of specifying the theme in manifest, I defined it in onCreatefor each activity that extends ActionBarActivity:

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

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

Here MyAppThemeis a descendant of Theme.AppCompat, and is defined in xml. Note that the theme must be set before super.onCreateand setContentView.

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

回答by Yamen Nassif

go to your styles and put the parent

转到您的样式并放置父项

parent="Theme.AppCompat"

instead of

代替

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

回答by Ali Gürelli

In my case i have no values-v21 file in my res directory. Then i created it and added in it following codes:

就我而言,我的 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>

回答by sharma_kunal

Change the theme of the desired Activity. This works for me:

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

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