Kotlin user dialog Parameter set as non-empty

I got this error:

Called: java.lang.IllegalArgumentException: parameter specified as non-null - null: kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull method, savedInstanceState parameter

When I try to inflate the user dialog in Kotlin, I got the error that I wrote above in the super.onCreate line in the dialog box.

dialogue code:

class Custom_Dialog_Exit_App(var activity: Activity)// TODO Auto-generated constructor stub : Dialog(activity, R.style.full_screen_dialog) { override fun onCreate(savedInstanceState: Bundle) { super.onCreate(savedInstanceState) requestWindowFeature(Window.FEATURE_NO_TITLE) setContentView(R.layout.custom_dialog_exit_app) activity.window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) initView() } fun initView() { initClicks() } fun initClicks() { } } 

and init:

 val omer = Custom_Dialog_Exit_App( this@MainActivity ) omer.show() 

Please, help

+5
source share
3 answers

override fun onCreate(savedInstanceState: Bundle) {

Since savedInstanceState may be null , should the type be Bundle? .

When you indicate that the parameter is non-zero, kotlin generates a check in all cases. This includes the implementation of the Java interface, so you need to be careful when entering null parameters other than zero.

+10
source

I also encounter an error, I changed the Bundle type to "Bundle?" . Then he worked for me. In Kotlin you need to indicate if the variable / parameter is null or not.

 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) init() } 
+1
source

change this line

  activity.window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) 

to

 if(activity.window != null) { activity.window!!.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT) } else { Log.e(TAG, "Window is null"); } 
-2
source

Source: https://habr.com/ru/post/1268931/


All Articles