Skip to content
Advertisement

How to build cordova project with Kotlin

I have a cordova project folder www already in progress.

Build this and you will have an Android project.

This will create a MainActivity.java file.

I don’t know Java so I changed the file to MainActivity.kt and converted the internal code.

But when I run it, it doesn’t run as error.

Build cordova:

cordova build android

Created MainActivity.java:

package com.example.gamename;
import android.os.Bundle;
import org.apache.cordova.*;
public class MainActivity extends CordovaActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Bundle extras = getIntent().getExtras();
        if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
            moveTaskToBack(true);
        }
        loadUrl(launchUrl);
    }
}

converted MainActivity.kt:

package com.example.gamename
import android.content.Intent.getIntent
import android.os.Bundle
import org.apache.cordova.*
class MainActivity:CordovaActivity() {
  override fun onCreate(savedInstanceState:Bundle) {
    super.onCreate(savedInstanceState)
    val extras = getIntent().getExtras()
    if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
      moveTaskToBack(true)
    }
    loadUrl(launchUrl)
  }
}

Get error….:

2020-03-09 18:56:57.159 7625-7625/com.example.gamename E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.gamename, PID: 7625 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gamename/com.example.gamename.MainActivity}: java.lang.IllegalArgumentException: Parameter specified as non-null is null: method kotlin.jvm.internal.Intrinsics.checkParameterIsNotNull, parameter savedInstanceState

Is there a solution? Or is there no way?

It must be resolved to use authentication, payment, and db (unfortunately I don’t know Java).

Advertisement

Answer

Need to change Bundle into “Bundle?” in MainActivity.kt.

override fun onCreate(savedInstanceState:Bundle?) {
User contributions licensed under: CC BY-SA
6 People found this is helpful
Advertisement