【问题标题】:Layout Preview is not displaying layout when Material Design is added添加材料设计时布局预览不显示布局
【发布时间】:2019-12-22 10:37:43
【问题描述】:

我正在开发一个 Android 应用程序。在我决定在我的主题中使用 Material Design 之前,一切似乎都很完美。添加Material Design主题后,布局预览不显示预览,但在手机上构建时可以正常工作。删除 xml 中使用的材料设计组件时,一切正常。

附上我的代码:

activity_home.xml

     <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="@color/background"
        tools:context=".home.Home">


        <com.google.android.material.button.MaterialButton
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_centerInParent="true"
            android:id="@+id/logout"
            android:text="LOG OUT"
            />
    </RelativeLayout>

HomeActivity.java

package com.****.*****.home;


import androidx.appcompat.app.AppCompatActivity;
import com.hashimshafiq.prepup.R;
import butterknife.OnClick;

public class Home extends AppCompatActivity  {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);
    }

    @OnClick(R.id.logout) void onClickLogout(){

    }


}

build.graple(应用模块)

apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
    compileSdkVersion 28
    defaultConfig {
        applicationId "com.****.*****"
        minSdkVersion 19
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
        }

        compileOptions {
            sourceCompatibility JavaVersion.VERSION_1_8
            targetCompatibility JavaVersion.VERSION_1_8
        }
    }
    buildToolsVersion = '28.0.3'
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.android.support:design:28.0.0'
    implementation 'androidx.annotation:annotation:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0'
    implementation 'com.google.firebase:firebase-database:16.0.4'
    implementation 'com.google.firebase:firebase-auth:16.0.5'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    implementation "com.google.android.material:material:1.1.0-alpha09"
    implementation 'com.jakewharton:butterknife:10.1.0'
    annotationProcessor 'com.jakewharton:butterknife-compiler:10.1.0'
    implementation 'com.facebook.android:facebook-android-sdk:5.2.0'
}

build.gradle(预科)

// Top-level build file where you can add configuration options common to all sub-projects/modules.

buildscript {
    repositories {
        google()
        jcenter()

    }
    dependencies {
        classpath 'com.android.tools.build:gradle:3.4.2'
        classpath 'com.google.gms:google-services:4.2.0'

        // NOTE: Do not place your application dependencies here; they belong
        // in the individual module build.gradle files
    }
}

allprojects {
    repositories {
        google()
        jcenter()
        mavenCentral()

    }
}

task clean(type: Delete) {
    delete rootProject.buildDir
}

styles.xml

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.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>


    <style name="AppTextField" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">

        <item name="boxBackgroundColor">@color/background</item>
        <item name="boxStrokeColor">@android:color/white</item>
        <item name="hintTextColor">@android:color/white</item>


    </style>




</resources>

在布局预览中多发生的错误

java.lang.IllegalArgumentException: java.lang.ClassCastException@b83d5b8 在 sun.reflect.GeneratedMethodAccessor509.invoke(未知来源) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 android.animation.PropertyValuesHolder_Delegate.callMethod(PropertyValuesHolder_Delegate.java:108) 在 android.animation.PropertyValuesHolder_Delegate.nCallFloatMethod(PropertyValuesHolder_Delegate.java:143) 在 android.animation.PropertyValuesHolder.nCallFloatMethod(PropertyValuesHolder.java) 在 android.animation.PropertyValuesHolder.access$400(PropertyValuesHolder.java:38) 在 android.animation.PropertyValuesHolder$FloatPropertyValuesHolder.setAnimatedValue(PropertyValuesHolder.java:1387) 在 android.animation.ObjectAnimator.animateValue(ObjectAnimator.java:990) 在 android.animation.ValueAnimator.setCurrentFraction(ValueAnimator.java:674) 在 android.animation.ValueAnimator.setCurrentPlayTime(ValueAnimator.java:637) 在 android.animation.ValueAnimator.start(ValueAnimator.java:1069) 在 android.animation.ValueAnimator.start(ValueAnimator.java:1088) 在 android.animation.ObjectAnimator.start(ObjectAnimator.java:852) 在 android.animation.ValueAnimator.startWithoutPulsing(ValueAnimator.java:1081) 在 android.animation.AnimatorSet.handleAnimationEvents(AnimatorSet.java:1142) 在 android.animation.AnimatorSet.startAnimation(AnimatorSet.java:1227) 在 android.animation.AnimatorSet.start(AnimatorSet.java:729) 在 android.animation.AnimatorSet.start(AnimatorSet.java:684) 在 android.animation.StateListAnimator.start(StateListAnimator.java:188) 在 android.animation.StateListAnimator.setState(StateListAnimator.java:181) 在 android.view.View.drawableStateChanged(View.java:21105) 在 android.widget.TextView.drawableStateChanged(TextView.java:5283) 在 androidx.appcompat.widget.AppCompatButton.drawableStateChanged(AppCompatButton.java:156) 在 android.view.View.refreshDrawableState(View.java:21160) 在 android.view.View.dispatchAttachedToWindow(View.java:18379) 在 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404) 在 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404) 在 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404) 在 android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3404) 在 android.view.AttachInfo_Accessor.setAttachInfo(AttachInfo_Accessor.java:42) 在 com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:335) 在 com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:391) 在 com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:195) 在 com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:540) 在 com.android.tools.idea.rendering.RenderTask.lambda$inflate$5(RenderTask.java:666) 在 java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1590) 在 java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) 在 java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) 在 java.lang.Thread.run(Thread.java:745)

注意:应用在 Moble 上运行良好,但布局预览未显示布局,这就是问题所在。

提前致谢。

编辑 我已经完成了以下解决方案,但没有任何反应并且错误仍然存​​在。

  • 使缓存无效并重新启动
  • 干净的构建
  • 将材料设计恢复到版本 1.0.0

【问题讨论】:

  • 你的 Android Studio 的版本是多少??
  • Android Studio 版本 3.4.2
  • 尝试从预览窗口更改您的 AppTheme
  • 当我从设计屏幕更改 AppTheme 时,它​​显示布局但颜色显示不正确。另外,当我从设计模式切换到文本模式并打开侧面预览屏幕时,错误仍然存​​在并且没有显示预览

标签: android xml android-studio material-design


【解决方案1】:

在以下更改后刷新布局:

1) 从文件 activity_home.xml 的父布局中删除行 tools:context=".home.Home"

2) 在文件 HomeActivity.java 中将类 Home 的名称更改为 HomeActivity

3) 使用 gradle 文件重新同步项目。

编辑:

这不是由于 XML/后端代码中的错误。这是由于 Android Studio 中的一个错误导致的,可以跟踪 here 它显然也已在版本 3.6 Canary 3(API 29 和 material_version = '1.1.0-alpha08')中得到修复。

【讨论】:

  • 完成所有步骤,但仍不显示预览。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-02
  • 1970-01-01
相关资源
最近更新 更多