You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TextInput与launch_screen文件引发RelativeLayout错误排查求助

排查并解决React Native中TextInput引发的RelativeLayout错误

调试器抛出的错误信息:

android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class RelativeLayout
Caused by: java.lang.ClassNotFoundException: Didn't find class "android.view.RelativeLayout" on path...

经排查,该错误由TextInput组件引发,移除该组件后错误消失。

出现问题的Form组件代码

import { Formik } from "formik";
import { Button, TextInput, View } from 'react-native';
import { FormValues } from "./types";
import Box from "../../../../components/Box";

const Form = (): JSX.Element => {

    const initialValues: FormValues = {
        first_name: "",
        last_name: "",
        email: "",
        password: "",
        github_profile: "",
        linkedin_profile: ""
    }

    const onSubmit = () => {
        // handle submit
    }

    return (
        <Box style={{flex: 1, backgroundColor: 'black'}}>
            <Formik
            initialValues={initialValues}
            onSubmit={onSubmit}
        >
            {({ handleChange, handleBlur, handleSubmit, values }) => (
            <View>
                <TextInput
                onChangeText={handleChange('email')}
                onBlur={handleBlur('email')}
                value={values.email}
                />
                <Button onPress={() => handleSubmit()} title="Submit" />
            </View>
            )}
        </Formik>
        </Box>
        
    )
}

export default Form;

launch_screen文件代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@drawable/launch_screen" android:scaleType="centerCrop" />
</RelativeLayout>

解决方案

1. 替换布局组件

RelativeLayout在AndroidX架构中已逐步被ConstraintLayout取代,直接修改launch_screen.xml的布局根节点:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView 
        android:layout_width="match_parent" 
        android:layout_height="match_parent" 
        android:src="@drawable/launch_screen" 
        android:scaleType="centerCrop" />
</androidx.constraintlayout.widget.ConstraintLayout>

2. 确认依赖配置

检查Android项目的app/build.gradle文件,确保已引入ConstraintLayout依赖(版本可根据项目需求调整):

implementation 'androidx.constraintlayout:constraintlayout:2.1.4'

3. 清理缓存并重建

执行Android端清理命令,清除旧构建缓存后重新编译项目:

./gradlew clean

内容的提问来源于stack exchange,提问作者Homero Gazze

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 18:28:28