【问题标题】:Why do I receive "as non-null is null" error after Android Studio converts Java code into Kotlin code automatically?为什么在 Android Studio 自动将 Java 代码转换为 Kotlin 代码后,我会收到“as non-null is null”错误?
【发布时间】:2018-06-03 03:46:48
【问题描述】:

当我将代码 B(Java 代码)复制并粘贴到 Android Studio 3.1.2 中时,我选择自动转换为 Kotlin 代码。

所以我在 Kotlin 中得到了显示的代码 A,但出现以下错误。为什么?

为什么 Android Studio 自动将 Java 代码转换为 Kotlin 代码时会出现该错误?

顺便说一句,代码 B(Java 代码)运行良好。

错误

java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter state
at ui.DialogChangePassword.showDialog(DialogChangePassword.kt)

代码 A(Kotlin 代码)

class DialogChangePassword(context: Context, attrs: AttributeSet) : DialogPreference(context, attrs) {
    private var mView: View? = null

    init {
        dialogLayoutResource = R.layout.item_custom_password_dialog
    }

    override fun onCreateDialogView(): View? {
        // TODO Auto-generated method stub
        mView = super.onCreateDialogView()
        return mView
    }


    override fun onDialogClosed(positiveResult: Boolean) {
        super.onDialogClosed(positiveResult)
    }

    override fun showDialog(state: Bundle) {
        // Call show on default first so we can
        // override the handlers
        super.showDialog(state)

        val d = dialog as AlertDialog
        d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener {
            if (IsInputOKAndSavePassword()) {
                d.dismiss()
            }
        }
    }

    private fun IsInputOKAndSavePassword(): Boolean {

        return true
    }

}

代码 B(Java 代码)

public class DialogChangePassword extends DialogPreference {
    private View mView;

    public DialogChangePassword(Context context, AttributeSet attrs) {
        super(context, attrs);
        setDialogLayoutResource(R.layout.item_custom_password_dialog);
    }

    @Override
    protected View onCreateDialogView() {
        // TODO Auto-generated method stub
        mView = super.onCreateDialogView();
        return mView;
    }


    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);
    }

    @Override
    protected void showDialog(Bundle state) {
        // Call show on default first so we can
        // override the handlers
        super.showDialog(state);

        final AlertDialog d = (AlertDialog) getDialog();
        d.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (IsInputOKAndSavePassword()){
                            d.dismiss();
                        }
                    }
                });
    }


    private boolean IsInputOKAndSavePassword(){
        boolean result=true;

        return result;
    }


}

【问题讨论】:

    标签: android kotlin


    【解决方案1】:

    Kotlin 通过区分可空类型(例如 Bundle?)和不可空类型(例如 Bundle)将 null 视为一等公民。

    如您所知,Bundle 实例可以为 null,具体取决于您当前处于组件生命周期的哪个阶段。例如,第一次创建 Activity 实例时,onCreate() 被调用null Bundle 因为没有要恢复的状态。但是,如果由于配置更改而重新创建了相同的Activity,则可以使用非空Bundle 实例调用onCreate()(您可能在其中存储了一些数据以帮助重新创建关联的屏幕)。

    鉴于此,showDialog 签名的编写方式应允许state 可能为空,如下所示:

        override fun showDialog(state: Bundle?) {
            ...
        }
    

    希望有帮助!

    【讨论】:

    • 谢谢!你的代码很棒。是不是意味着有时候Android Studio 3.1.2自动转换的kotin代码并不总是正确的?
    • 鉴于这个例子,更不用说生成跨语言代码的雄心壮志了,我认为可以安全地假设结果应该更多地用作模板或指导,而不是 100% 生产- 准备好的代码
    猜你喜欢
    • 2020-06-07
    • 2018-04-16
    • 1970-01-01
    • 2021-08-22
    • 2016-09-05
    • 1970-01-01
    • 2021-06-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多