0

Am trying to call a custom dialog box but the app crashes on this line:

cd.show()

This is how I'm calling it:

val cd = CustomDialog(this, applicationContext)
    cd.show()

This is the error:

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

I followed this example and it works in java but fails after converting to kotlin

WanjyDan
  • 69
  • 1
  • 1
  • 10

2 Answers2

2

From your linked example:

@Override
protected void onCreate(Bundle savedInstanceState) {

In Kotlin, this should have become

override fun onCreate(savedInstanceState: Bundle?) {

Note the question mark, judging by your error message it's probably missing in your Kotlin code. The type Bundle does not accept null as a legal value (it's non-nullable) and adding a question mark to it broadens it to accept null values.

The activity can be started with no saved instance state, such as when first starting it after installation.

Marko Topolnik
  • 179,046
  • 25
  • 276
  • 399
0

You just need to pass context when initializing CustomDialog. For example, try this code:

val cd = CustomDialog(this@YourActivity) //Assuming you are initializing it in Activity.
cd.show()
Aseem Sharma
  • 1,205
  • 10
  • 18