You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Cordova项目转Kotlin后启动报错,求解决方案

Fixing Your Cordova MainActivity.kt Crash

Hey there, let's get your Cordova project running with Kotlin smoothly. The crash you're seeing has two straightforward fixes, and I'll also share some tips for your upcoming auth, payment, and database work since you're not familiar with Java.

What's Causing the Crash?

Your error message points to two key issues:

  1. Non-null parameter violation: In Kotlin, parameters are non-null by default. Your onCreate method declared savedInstanceState: Bundle as non-null, but Android often passes null here (like when launching the app for the first time). Kotlin's strict null safety checks throw an exception when this mismatch happens.
  2. Incorrect Intent access: You tried using a static getIntent() method, but Android Activities have a built-in intent property you should use instead. That unnecessary import was causing confusion and incorrect behavior.

Corrected MainActivity.kt Code

Here's the fixed version of your Kotlin file, with comments explaining each change:

package com.example.gamename

import android.os.Bundle
import org.apache.cordova.CordovaActivity

class MainActivity : CordovaActivity() {
    // Mark savedInstanceState as nullable with ? to align with Android's behavior
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        
        // Use the Activity's built-in intent property instead of static getIntent()
        val extras = intent.extras
        if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
            moveTaskToBack(true)
        }
        
        loadUrl(launchUrl)
    }
}

Quick Breakdown of Changes

  • savedInstanceState: Bundle?: Adding ? tells Kotlin this parameter can be null, which matches how Android starts Activities (it passes null when launching the app for the first time).
  • Removed import android.content.Intent.getIntent: Replaced with intent.extras—this is the standard, clean way to access the current Activity's Intent data in Android (works for both Java and Kotlin).

Tips for Your Auth, Payment, and Database Goals

Since you don't know Java and want to add these features without writing too much native code:

  • Prioritize Cordova plugins first: Most common features have pre-built plugins that let you work entirely in JavaScript. For example:
    • Database: Use cordova-plugin-sqlite-2 for local storage, or a Firebase Firestore Cordova plugin for cloud-based data.
    • Authentication: cordova-plugin-firebase-auth or cordova-plugin-googleplus let you add social auth without touching native code.
    • Payments: cordova-plugin-stripe handles Stripe payments directly from your JS code.
  • If you need custom Kotlin logic: Start small—write simple Kotlin functions and wrap them in a basic custom Cordova plugin so you can call them from JavaScript. Plugins should be your last resort only if no existing solution fits your needs.

内容的提问来源于stack exchange,提问作者msm082919

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.06 22:07:40