尝试在 react-native 中使用 Button 时出错 [英] Error when trying to use Button in react-native
问题描述
在制作一个简单的 react-native 程序时,我无法让 组件工作.在
react-native run-android
之后我总是得到的错误是
When making a simple react-native program I cannot get the <Button>
component to work. The error I always get after react-native run-android
is
java.lang.String 不能强制转换为com.facebook.react.uimanager.AccessibilityDelegateUtil$AccessibilityRole
java.lang.String cannot be cast to com.facebook.react.uimanager.AccessibilityDelegateUtil$AccessibilityRole
当我省略按钮时,一切正常,我可以点击文本就好了.
When I omit the button, everything works fine, and I can click the Text just fine.
代码:
import React, {Component} from 'react';
import {Platform, StyleSheet, Text, View, Button } from 'react-native';
type Props = {};
export default class App extends Component<Props> {
render() {
return (
<View style={styles.container}>
<Button onPress={()=>console.log('hi')} title="go"/>
<Text onPress={()=>console.log('hi')}>Welcome to React Native!</Text>
</View>
);
}
}
一些依赖:
"dependencies": {
maar eens alles goed staat hoop ik gewoon dat de miserie gedaan is, cf "react": "16.6.0-alpha.8af6728",
"react-native": "0.57.3"
},
推荐答案
我刚刚在 0.55
版本上测试了您的代码.这是完全正确的并且工作正常.您的 react-native
版本可能有误.
I've just tested your code on version 0.55
. It was perfectly correct and worked fine.
I'm likely to have error with your react-native
version.
初始化低版本
react-native init --version="react-native@0.55.0" YOUR_APP_NAME
在那之前也试试这个
<Button
onPress={() => alert('hi')}
title="Press Me"
>
Press Me
</Button>
这篇关于尝试在 react-native 中使用 Button 时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!