wanjydan
wanjydan

Reputation: 139

How to call a custom dialog box with Kotlin? Fails after converting java to kotlin

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

Upvotes: 0

Views: 600

Answers (2)

Aseem Sharma
Aseem Sharma

Reputation: 1853

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()

Upvotes: 0

Marko Topolnik
Marko Topolnik

Reputation: 200168

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.

Upvotes: 2

Related Questions