Android Studio中的渲染问题

3

我是 Android Studio 的新手。在 activity_main.xml 标签下,我遇到了这个问题。那么,如何解决呢?我已经离线安装了 Android Studio 1.4.1 包。另外,在 activity_main.xml 中,我无法将小部件拖放到手机上。但是,在 content_main.xml 中可以进行拖放。

 Rendering Problems
 The following classes could not be instantiated:
- android.support.design.widget.CoordinatorLayout (Open Class, Show Exception, Clear Cache)
- android.support.design.widget.AppBarLayout (Open Class, Show Exception, Clear Cache)
 Tip: Use View.isInEditMode() in your custom views to skip code or show sample data when shown in the IDE 

Exception Details  

 java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.  
 at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)  
 at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:178)   
 at android.support.design.widget.CoordinatorLayout.<init>(CoordinatorLayout.java:172)  
 at java.lang.reflect.Constructor.newInstance(Constructor.java:422)   
 at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)   
 at android.view.LayoutInflater.inflate(LayoutInflater.java:492)   
 at android.view.LayoutInflater.inflate(LayoutInflater.java:394) 
  Copy stack to clipboard

这里是我的activity_main.xml文本:

在这里放置XML代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"  android:layout_width="match_parent"
android:layout_height="match_parent" android:fitsSystemWindows="true"
tools:context=".MainActivity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"
    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"
        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<android.support.design.widget.FloatingActionButton android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_dialog_email" />
   </android.support.design.widget.CoordinatorLayout>

你能发布一下你的activity_main.xml文件吗? - Gi0rgi0s
@Gi0rgi0s 你好,我已经发布了,请看一下。 - Subash Aryal
4个回答

1
父级主题 AppTheme 需要是子级或者 Theme.AppCompat。请检查你代码中的样式:@style/AppTheme

1

由于你的MainActivity扩展了AppCompatActivity,因此它的主题必须来自AppCompat。为解决此问题,请执行以下操作:

  1. 打开文件app/res/values/styles.xml
  2. 更改styles.xml文件中的以下行以如下方式显示:

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

<style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />.

他们都需要从AppCompat继承以适合您选择的布局。请参见默认工作的styles.xml文件。您可以将其复制并粘贴到您的styles.xml文件中。

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>
    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>
    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
</resources>
  1. 如果您仍然遇到问题,请转到app/manifests/AndroidManifest.xml文件,确保主题的值看起来像这样android:theme="@style/AppTheme.NoActionBar"或者android:theme="@style/AppTheme"这样他们就可以从上面列出的AppCompat主题中继承。

此外,也可以参考这里:You need to use a Theme.AppCompat theme (or descendant) with this activity


styles.xml和你说的一样。我的意思是它完全符合你所说的。但我仍然在activity_main.xml下遇到了错误。 - Subash Aryal
如果可以的话,请清理项目,然后尝试我帖子底部链接中的解决方案。 - Gi0rgi0s
请参考以下链接了解如何清理代码:https://dev59.com/Yl4b5IYBdhLWcg3w5VE9 如果这不起作用,您可能需要粘贴更多的代码。我保证这个问题有一个简单的解决方案。 - Gi0rgi0s
@SubashAryal,请告诉我们您是如何解决这个问题的。 - Gi0rgi0s

0

我曾经遇到同样的问题,尝试了很多方法,但最终解决问题的方法是将 Android Studio 版本从 3.1.1 更新到 3.2。希望这能帮到你。


0
android:theme="@style/Theme.AppCompat.Light" 添加到 CoordinatorLayout 中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接