在 android studio 中设计 xml 文件的预览不会“打开"; [英] Desing preview of an xml file in android studio wont "open"

查看:24
本文介绍了在 android studio 中设计 xml 文件的预览不会“打开";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在制作的 android 项目中遇到了问题.更新 android studio 后,我的 .xml 文件设计预览之一将无法打开.我收到 2 个错误.我已经搜索了解决方案,并且已经尝试了很多我在网上找到的东西,但没有一个能改变这种情况.

I have come across a problem with an android project I am making. After I updated android studio one of my .xml files design preview won't open. I am getting 2 errors. I have searched for solutions and have already tried a lot of stuff I've found online but none of them have changed the situation.

第一个是:

Render problem:
java.lang.reflect.InvocationTargetException
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.base/java.lang.reflect.Method.invoke(Method.java:566)
    at android.view.BridgeInflater.lambda$createViewFromCustomInflater$0(BridgeInflater.java:259)
    at android.view.BridgeInflater.createViewFromCustomInflater(BridgeInflater.java:285)
    at android.view.BridgeInflater.onCreateView(BridgeInflater.java:122)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:928)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:948)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1002)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:309)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:354)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:710)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:865)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:174)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)
Caused by: java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:1024)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:809)
    at android.content.res.BridgeTypedArray.getValue(BridgeTypedArray.java:778)
    at android.content.res.BridgeTypedArray.peekValue(BridgeTypedArray.java:847)
    at android.view.View.<init>(View.java:5951)
    at android.widget.TextView.<init>(TextView.java:996)
    at android.widget.EditText.<init>(EditText.java:87)
    at android.widget.EditText.<init>(EditText.java:83)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:74)
    at androidx.appcompat.widget.AppCompatEditText.<init>(AppCompatEditText.java:69)
    at androidx.appcompat.app.AppCompatViewInflater.createEditText(AppCompatViewInflater.java:209)
    at androidx.appcompat.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:127)
    ... 27 more

第二个是:

String index out of range:-1
Error inflating the preview (Details):
java.lang.StringIndexOutOfBoundsException: String index out of range: -1
    at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:47)
    at java.base/java.lang.String.charAt(String.java:693)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:1024)
    at android.content.res.BridgeTypedArray.getType(BridgeTypedArray.java:809)
    at android.content.res.BridgeTypedArray.getValue(BridgeTypedArray.java:778)
    at android.content.res.BridgeTypedArray.peekValue(BridgeTypedArray.java:847)
    at android.view.View.<init>(View.java:5951)
    at android.view.ViewGroup.<init>(ViewGroup.java:697)
    at android.widget.FrameLayout.<init>(FrameLayout.java:99)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:55)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:51)
    at com.android.layoutlib.bridge.MockView.<init>(MockView.java:47)
    at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:324)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959)
    at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:1121)
    at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:72)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:1095)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:680)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:499)
    at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:354)
    at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:431)
    at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:141)
    at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:710)
    at com.android.tools.idea.rendering.RenderTask.lambda$inflate$6(RenderTask.java:865)
    at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$2.run(RenderExecutor.kt:174)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:834)

在更新完成之前,这两个错误不存在.

Those two erros were not present before the update was done.

XML 文件的代码为:

The code of the XML file is:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:visibility="visible"
    tools:context=".Calculate">

    <EditText
        android:id="@+id/height"
        android:layout_width="237dp"
        android:layout_height="50dp"
        android:autofillHints="none"
        android:ems="10"
        android:hint="@string/insert_your_height_in_cm"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.499" />

    <EditText
        android:id="@+id/weight"
        android:layout_width="237dp"
        android:layout_height="50dp"
        android:layout_marginTop="20dp"
        android:autofillHints=""
        android:ems="10"
        android:hint="@string/insert_your_weight_in_kgr"
        android:inputType="number"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/height"
        app:layout_constraintVertical_bias="0.033" />

    <TextView
        android:id="@+id/message"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginBottom="10dp"
        android:text="@string/bmi_calculator"
        app:layout_constraintBottom_toTopOf="@+id/bmiImage"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.498"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.358" />

    <Button
        android:id="@+id/Calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="CalculateBMI"
        android:text="@string/calculate"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.136"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weight"
        app:layout_constraintVertical_bias="0.714" />

    <TextView
        android:id="@+id/warning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:textColor="#FA0C0C"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/Calculate"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/switch1"
        app:layout_constraintVertical_bias="0.588" />

    <androidx.appcompat.widget.SwitchCompat
        android:id="@+id/switch1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="36dp"
        android:textColor="#0099FF"
        android:text="@string/gender"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/textView2"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toEndOf="@+id/textView"
        app:layout_constraintTop_toBottomOf="@+id/weight"
        app:layout_constraintVertical_bias="0.0" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/male"
        android:textColor="#0099FF"
        app:layout_constraintBottom_toTopOf="@+id/warning"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.228"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weight"
        app:layout_constraintVertical_bias="0.362" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="16dp"
        android:text="@string/female"
        android:textColor="#D1D1D1"
        app:layout_constraintBottom_toTopOf="@+id/warning"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.796"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/weight"
        app:layout_constraintVertical_bias="0.362" />

    <ImageView
        android:id="@+id/bmiImage"
        android:layout_width="237dp"
        android:layout_height="195dp"
        android:layout_marginTop="50dp"
        android:layout_marginBottom="31dp"
        android:contentDescription="weight images"
        android:visibility="invisible"
        app:layout_constraintBottom_toTopOf="@+id/userName"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/ic_launcher_background" />

    <EditText
        android:id="@+id/userName"
        android:layout_width="239dp"
        android:layout_height="45dp"
        android:layout_marginBottom="20dp"
        android:ems="10"
        android:hint="Insert your name"
        android:inputType="textPersonName"
        app:layout_constraintBottom_toTopOf="@+id/height"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.494"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <Button
        android:id="@+id/saveBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="73dp"
        android:onClick="saveBtn"
        android:text="@string/save_entry"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.351"
        app:layout_constraintStart_toEndOf="@+id/Calculate"
        app:layout_constraintTop_toBottomOf="@+id/weight"
        app:layout_constraintVertical_bias="0.714" />
</androidx.constraintlayout.widget.ConstraintLayout>

推荐答案

我之前遇到过同样的问题,经过大量搜索,我知道 EditText 中的 属性 android:autofillHints 存在问题.请尝试将其注释掉或删除并尝试检查您的布局,它会解决此问题.

Same issue i was facing earlier after searching alot i get to know that there was an issue with the property android:autofillHints in EditText.please try to comment it out or delete and try to check your layout it'll fix this.

这篇关于在 android studio 中设计 xml 文件的预览不会“打开";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆