将null作为vararg参数的一部分传递给Kotlin的Java方法

时间:2018-03-01 14:29:27

标签: kotlin kotlin-interop

我在Java中有类似于这个方法声明的东西。假设该类名为public T execute(String s, Function<T> f, Object... args) {}

val arg1 = ""
val arg2: String? = null
val javaObject = JavaClass()

javaObject.execute("some string", { print(it) }, arg1, arg2)

如果我想从Kotlin打电话给我(我的版本是1.2.21),我可以写一些类似的东西:

IllegalStateException

奇怪的是,我在执行此代码时得到arg2 must not be null。它说args。我能理解它是否说["", null]不能为空。但考虑到这个调用会导致数组看起来像null,这在Java中完全没问题,这看起来很奇怪。

我需要能够将 const MyTabnav = TabNavigator({ScreenOne: { screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />, navigationOptions: { tabBarLabel: 'ScreenOne', tabBarIcon: ({ tintColor }) => ( <View style={[styles.tabViewBox]}> <Text style={[styles.tabText, { color: tintColor }]}>ScreenOne</Text> </View> ) } }, ScreenTwo: { screen: ({ navigation, screenProps }) => <ScreenOneNav screenProps={{ tabbarNavigation: navigation, ...screenProps, }} onNavigationStateChange={null} />, navigationOptions: { tabBarLabel: 'ScreenOne', tabBarIcon: ({ tintColor }) => ( <View style={[styles.tabViewBox]}> <Text style={[styles.tabText, { color: tintColor }]}>ScreenTwo</Text> </View> ) } }, ScreenThree: { screen: ({ navigation, screenProps }) => <StockNotificationNav screenProps={{ tabbarNavigation: navigation, ...screenProps }} onNavigationStateChange={null} />, navigationOptions: { tabBarLabel: 'Notifications', tabBarIcon: ({ tintColor }) => ( <View style={[styles.tabViewBox]}> <Text style={[styles.tabText, { color: tintColor }]}>ScreenThree</Text> </View> ) } }, }, { tabBarOptions: { style: { backgroundColor: white, height: 55, borderTopColor: 'transparent', borderTopWidth: 1, paddingRight: 10, paddingLeft: 10, borderTopWidth: 1, borderTopColor: grayPlaceHolder }, showLabel: false, showIcon : true, }, //Here Goes Your CustomTabBar Component tabBarComponent : CustomTabBarComponent, initialRouteName: 'ScreenTwo', tabBarPosition: 'bottom', animationEnabled: false, swipeEnabled: false }, []); 值传递给此方法。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:2)

为此类调用抛出的IllegalStateException意味着该方法返回null,并且您将返回值分配给非null类型的变量。它与varargs无关。