【问题标题】:json value '2' of type nsnumber cannot be converted to nsstringnsnumber 类型的 json 值“2”无法转换为 nsstring
【发布时间】:2019-01-19 10:47:15
【问题描述】:

我正在使用从库中导入的 Carousel 制作 imageSlider:“react-native-banner-carousel”

我正在尝试获取本地存储在文件夹组件中的图像。我正在创建一个数组 if 图像,然后尝试通过 Carousel 呈现它们。

我肯定会收到类似的错误:“nsnumber 类型的 json 值 '2' 无法转换为 nsstring”

ImageSlider 代码:

const BannerWidth = Dimensions.get('window').width;
const BannerHeight = 260;

const images = [
    require("./abc.jpg"),
    require("./xyz.jpg")
];

export default class ImageSlider extends React.Component {
    renderPage(image, index) {
        return (
            <View key={index}>
                <Image style={{ width: BannerWidth, height: BannerHeight }} source={{uri:image}} /> 
            </View>
        ); 
    }

render() {
    return (
        <View style={styles.container}>
            <Carousel
                autoplay
                autoplayTimeout={5000}
                loop
                index={0}
                pageSize={BannerWidth}
            >
                {images.map((image, index) => this.renderPage(image, index))}
            </Carousel>
        </View>
    );
}
}

    const styles = StyleSheet.create({
        container: {
            flex: 1,
            backgroundColor: '#fff',
            justifyContent: 'center'
        },
    });

因此,我应该能够渲染图像数组中显示的所有图像。但我收到了这个错误。 我哪里错了?

【问题讨论】:

    标签: react-native carousel


    【解决方案1】:

    您的问题在这里:

    const images = [
        require("./abc.jpg"),
        require("./xyz.jpg")
    ];
    

    require 不返回图片的 URL,而是返回媒体的内部索引。在你的情况下,我假设是 2,并且图像的界面需要一个字符串。

    试试这个:

    <Image style={{ width: BannerWidth, height: BannerHeight }} source={image} />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-10
      • 1970-01-01
      • 1970-01-01
      • 2019-05-02
      • 1970-01-01
      • 2016-01-22
      • 2017-10-30
      相关资源
      最近更新 更多