Java android studio sdk 22 版在渲染过程中出现异常:action_bar

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29034476/
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-11 07:14:24  来源:igfitidea点击:

android studio sdk version 22 exception during rendering: action_bar

javaandroidandroid-studiosdkrendering

提问by Ali Aras

When I create a new project on android studio it is not giving any problem. However, looking at the activity_main.xmldesign it is shows me this:

当我在 android studio 上创建一个新项目时,它没有出现任何问题。然而,看看activity_main.xml它的设计,它向我展示了这一点:

Rendering Problems Exception raised during rendering: action_bar.

渲染问题渲染过程中引发异常:action_bar。

when I change sdk level 22 to 21 from the design page android studio shows nothing

当我从设计页面将 sdk 级别 22 更改为 21 时,android studio 什么也没显示

How can I solve this problem? This is not important problem but I wondered how can I fix this.

我怎么解决这个问题?这不是重要的问题,但我想知道如何解决这个问题。

回答by Oscar

I fixed it by changing buildToolsVersion "21.1.2", on build.gradle(Module: app),

我通过更改buildToolsVersion "21.1.2", on build.gradle(Module: app) 来修复它,

Do not use buildToolsVersion "22"

不使用 buildToolsVersion "22"

回答by Dave Fisher

Yeah just had to adjust the Android level in the drop down. The design tab preview in API level 22 is broken right now. I looked that the stack trace for the error and it has something to do with needing the alpha channel. I'm sure Google will fix it Monday. :) enter image description here

是的,只需在下拉菜单中调整 Android 级别即可。API 级别 22 中的设计选项卡预览现在已损坏。我查看了错误的堆栈跟踪,它与需要 alpha 通道有关。我相信谷歌会在周一修复它。:) 在此处输入图片说明

回答by Pranav2579

Please use build API21 to see the preview. I am on mac and build 22 is broken/not working for me. When i switched back to build 21, i could see the preview.!

请使用 build API21 查看预览。我在 mac 上,build 22 坏了/不适合我。当我切换回 build 21 时,我可以看到预览。!

回答by Reynolds R. G. G.

Through days of frustration, I've finally found out the answer lies in the build.gradle of my app module! I've changed the buildToolsVersionfrom 22to 21.1.2to properly solve that exact exception like yours. It seems anything to do with API 22: Android 5.1is giving hiccups at the moment. Hopefully Google will fix it soon.

经过几天的沮丧,我终于在我的应用程序模块的 build.gradle 中找到了答案!我已经将buildToolsVersionfrom22更改21.1.2为正确解决了像您这样的确切异常。API 22: Android 5.1此刻似乎与打嗝有关。希望谷歌尽快修复它。

Changing these lines in the build.gradle gave me a quick fix for this problem:

更改 build.gradle 中的这些行让我快速解决了这个问题:

android {
    compileSdkVersion 21
    buildToolsVersion "21.1.2"
}

回答by colintheshots

ActionBar is deprecated in favor of Toolbar, which was added in API 21. However, the base theme for Android Studio's templates still seems to use ActionBar-based themes.

ActionBar 已被弃用,取而代之的是在 API 21 中添加的 Toolbar。然而,Android Studio 模板的基本主题似乎仍然使用基于 ActionBar 的主题。

The solution is not to downgrade your build tools version, but rather to change your app's base theme. If you downgrade your build tools, you will miss out on support for the latest features added in Android 5.1 and later.

解决方案不是降级您的构建工具版本,而是更改应用程序的基本主题。如果您降级构建工具,您将错过对 Android 5.1 及更高版本中添加的最新功能的支持。

Your base theme is listed in AndroidManifest.xml under application/android:theme.

您的基本主题列在 AndroidManifest.xml 中的 application/android:theme 下。

Go to res/values/styles.xml and change the parent attribute to a different base theme that doesn't rely on the deprecated ActionBar. In the unlikely case your app is API 21+, you can use Theme.Material. Otherwise, you'll need to use Theme.AppCompat.NoActionBar or define your own base theme.

转到 res/values/styles.xml 并将父属性更改为不依赖于已弃用的 ActionBar 的不同基本主题。在不太可能的情况下,您的应用程序是 API 21+,您可以使用 Theme.Material。否则,您将需要使用 Theme.AppCompat.NoActionBar 或定义您自己的基本主题。

Here's a styles.xml example:

这是一个 style.xml 示例:

<style name="AppTheme.Base" parent="Theme.AppCompat">
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="android:windowNoTitle">true</item>
    <item name="windowActionBar">false</item>
</style>

If you use this, be sure to put a Toolbar element in your XML layouts where you want the ActionBar to appear. You'll need to customize the Toolbar with child XML tags. I recommend using the <include>tag. As it says in the Toolbar documentation, Google's new design principles call for a change in visual style:

如果您使用它,请确保在您希望 ActionBar 出现的 XML 布局中放置一个 Toolbar 元素。您需要使用子 XML 标记自定义工具栏。我建议使用<include>标签。正如工具栏文档中所说,谷歌的新设计原则要求改变视觉风格:

In modern Android UIs developers should lean more on a visually distinct color scheme for toolbars than on their application icon. The use of application icon plus title as a standard layout is discouraged on API 21 devices and newer.

在现代 Android UI 中,开发人员应该更多地依赖于工具栏的视觉上不同的配色方案,而不是他们的应用程序图标。在 API 21 和更新版本的设备上不鼓励使用应用程序图标和标题作为标准布局。

android.support.v7.widget.Toolbar is in the support library for pre-API 21 devices.

android.support.v7.widget.Toolbar 位于 API 21 之前设备的支持库中。

EDIT: As the first comment points out, Google has a blog explaining how to use the new Toolbar. http://android-developers.blogspot.kr/2014/10/appcompat-v21-material-design-for-pre.html

编辑:正如第一条评论指出的那样,谷歌有一个博客解释如何使用新的工具栏。http://android-developers.blogspot.kr/2014/10/appcompat-v21-material-design-for-pre.html

回答by Vimbainashe E Mushayikwa

It turns out that you cannot use buildToolsVersion "22"or targetSdkVersion 22because it fails to render properly. It seems it can only render the app when set to API 21 or lower at the moment because I was getting the same error and as soon as I changed it back to 21 it started working again. Just go into build.gradle(module:app) and change the API back to the latest version of API 21.

事实证明您无法使用buildToolsVersion "22"targetSdkVersion 22因为它无法正确渲染。似乎它目前只能在设置为 API 21 或更低时呈现应用程序,因为我遇到了相同的错误,并且一旦我将其改回 21,它就会再次开始工作。只需进入 build.gradle(module:app) 并将 API 更改回最新版本的 API 21。

回答by Deepanshu

Sorry about the issue guys. The reason for the issue is that the rendering library is now trying to use more of the actual code from the appcompat library, in order to be more accurate. However, this has exposed some bugs in the Android Studio when the project is not built.

很抱歉这个问题伙计们。问题的原因是渲染库现在试图使用更多来自 appcompat 库的实际代码,以便更准确。但是,这暴露了Android Studio在未构建项目时的一些错误。

There is no need to update your gradle files or change the styles as some of the answers suggest.

无需像某些答案所建议的那样更新您的 gradle 文件或更改样式。

The workaround is to build the project once and then refresh the rendering by clearing the cache. See the attached image for the refresh icon which clears the cache.

解决方法是构建一次项目,然后通过清除缓存来刷新渲染。有关清除缓存的刷新图标,请参阅附加图像。

Refresh Button Location

刷新按钮位置

回答by Pober Wong

I solved it by rebuilding the project or module.

我通过重建项目或模块解决了它。

回答by HerberthObregon

The solve:

解决办法:

Create a New folder values-v19

创建一个新文件夹 values-v19

Create a styles.xml in this folder

在这个文件夹中创建一个styles.xml

<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
    <!-- For actionBar n' statusbar -->
    <item name="colorPrimary">#COLOR/item>
    <item name="colorPrimaryDark">#COLOR</item>

    <!-- For items -->
    <item name="colorAccent">#COLOR</item>
</style>

and is all :)

就是全部:)

回答by Mark Keen

This answer worked for me:

这个答案对我有用:

MyActivity extends AppCompatActivity

Instead of:

代替:

MyActivity extends ActionBarActivity   // now deprecated

Go back to your preview, clear cache and refresh!

返回预览,清除缓存并刷新!