android - Android Studio 中的渲染问题

标签 android android-studio

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

最佳答案

由于您的 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

关于android - Android Studio 中的渲染问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33959478/

相关文章:

java - 从 Android 应用程序向 MySQL 数据库提交数据

java - Android动态壁纸服务-获取屏幕旋转

java - 我设置安卓 :exported but I am still getting error: android:exported needs to be explicitly specified for <receiver>

使用具有 Android 资源的模型类加载 UI 的 Android MVP 架构标准

android - 从另一个 Activity 关闭一个 Activity 时出现 NullPointerException

android - 如何用C++编写Android服务

java - AppEnginePlugin : Unsupported major. 次要版本 51.0 - appengine 1.9.17

android - Webview 应用程序在第一次安装应用程序时工作,但之后只显示空白屏幕

git - 重新创建Android项目后如何 merge Git存储库?

java.lang.OutOfMemoryError : OutOfMemoryError thrown while trying to throw OutOfMemoryError; no stack trace available 错误