【发布时间】: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;
}
}
【问题讨论】: