Kotlin - 如何将null值传递给片段newInstance方法?

时间:2018-01-08 06:45:05

标签: android kotlin

我使用Kotlin创建了一个片段,你知道如何在Java中设置片段args的方便静态方法。像这样:

public static MyFragment newInstance(Bundle args){
    MyFragment fragment = new MyFragment();
    fragment.setArguments(args);
    return fragment;
}

我正在尝试使用伴侣对象在Kotlin中完成此操作,因为它是静态的。以下是我到目前为止编写的内容:

companion object {
    fun newInstance(@Nullable b: Bundle): MyFragment {
        val frag = MyFragment()
        frag.arguments = b
        return frag
    }
}

我在想,如果我使用@Nullable注释,我将被允许传入null,但是当我调用MyFragment.newInstance(null)时,我一直收到以下错误:

  

致命的例外:主要                                                                        过程:com.mobile.MyApp.labs,PID:5758                                                                        java.lang.IllegalArgumentException:指定为非null的参数为null:方法kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull,参数b                                                                            在com.mobile.MyApp.ui.cart.fragments.MyFragment $ Companion.newInstance(MyFragment.kt:0)                                                                            在com.mobile.MyApp.ui.cart.fragments.CartDetailsFragment.onCtaClicked(CartDetailsFragment.java:521)                                                                            在com.mobile.MyApp.ui.cart.fragments.CartDetailsFragment_ViewBinding $ 1.doClick(CartDetailsFragment_ViewBinding.java:66)                                                                            at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)                                                                            在android.view.View.performClick(View.java:6256)                                                                            在android.view.View $ PerformClick.run(View.java:24701)                                                                            在android.os.Handler.handleCallback(Handler.java:789)                                                                            在android.os.Handler.dispatchMessage(Handler.java:98)                                                                            在android.os.Looper.loop(Looper.java:164)                                                                            在android.app.ActivityThread.main(ActivityThread.java:6541)                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                            在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240)                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

如何传递null

2 个答案:

答案 0 :(得分:6)

在Kotlin中,您不会传递@Nullable,而是在类型名称后面写一个问号(?)以将其标记为可为空。

fun newInstance(b: Bundle?): MyFragment

答案 1 :(得分:0)

Kotlin提供(?)处理null。看看

companion object {
    fun newInstance( b: Bundle?): MyFragment {
        val frag = MyFragment()
        frag.arguments = b
        return frag
    }
}

希望它会对你有所帮助。