簡體   English   中英

在Android Studio中渲染問題

[英]Rendering Issue in Android Studio

我是Android Studio的新手。我在activity_main.xml選項卡下遇到了這個問題。 那么,如何解決這個問題。我已經安裝了android studio 1.4.1 bundle。 此外,我無法將小部件拖放到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>

AppTheme的父AppTheme需要是child或Theme.AppCompat 請檢查樣式:代碼中的@style/AppTheme

由於您的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主題。

請參閱此處: 您需要在此活動中使用Theme.AppCompat主題(或后代)

CoordinatorLayout添加android:theme="@style/Theme.AppCompat.Light"

我有同樣的問題並嘗試了很多東西,但對我有用的只是將Android Studio版本從3.1.1更新到3.2,它解決了完全相同的問題。 希望這可以幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM