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

Kotlin中Android Activity无参构造函数缺失致启动崩溃问题

问题:Android中Kotlin类因无参构造函数导致运行崩溃

我正在用Kotlin编写简单程序熟悉该语言,代码在Android Studio中编译无异常,但真机运行时出现致命错误。

相关代码

package com.example.check_in

import android.content.ContentResolver
import android.content.Context
import android.database.Cursor
import android.net.Uri
import android.telephony.SmsManager
import android.util.Log
import android.content.ContextWrapper
import java.util.Date


class SmsTracker(context: Context) : ContextWrapper(context) {
   private val appContext: Context = applicationContext
   private val contentResolver: ContentResolver = appContext.contentResolver
   private var cursor: Cursor? = null
}

运行错误日志

PROCESS STARTED (26298) for package com.example.check_in
---------------------------- 2023-06-19 08:32:01.099 26298-26298 xample.check_in         com.example.check_in                 W
Redefining intrinsic method java.lang.Thread
java.lang.Thread.currentThread(). This may cause the unexpected use of
the original definition of java.lang.Thread
java.lang.Thread.currentThread()in methods that have already been
compiled. 2023-06-19 08:32:01.099 26298-26298 xample.check_in<br />
com.example.check_in                 W  Redefining intrinsic method
boolean java.lang.Thread.interrupted(). This may cause the unexpected
use of the original definition of boolean
java.lang.Thread.interrupted()in methods that have already been
compiled. 2023-06-19 08:32:01.102 26298-26298 Compatibil...geReporter
com.example.check_in                 D  Compat change id reported:
171979766; UID 10374; state: ENABLED 2023-06-19 08:32:01.110
26298-26298 ziparchive              com.example.check_in<br />
W  Unable to open
'/data/data/com.example.check_in/code_cache/.overlay/base.apk/classes3.dm':
No such file or directory 2023-06-19 08:32:01.112 26298-26298
xample.check_in         com.example.check_in                 W
ClassLoaderContext classpath size mismatch. expected=0, found=1 (PCL[]
|
PCL[/data/data/com.example.check_in/code_cache/.overlay/base.apk/classes3.dex*222021406])
2023-06-19 08:32:01.119 26298-26298 nativeloader<br />
com.example.check_in                 D  Configuring
classloader-namespace for other apk
/data/app/~~CFRupp1tiaLh-cqxRJlzeg==/com.example.check_in-3kRsT7WaNyILayLaLSV99Q==/base.apk.
target_sdk_version=33, uses_libraries=,
library_path=/data/app/~~CFRupp1tiaLh-cqxRJlzeg==/com.example.check_in-3kRsT7WaNyILayLaLSV99Q==/lib/arm64,
permitted_path=/data:/mnt/expand:/data/user/0/com.example.check_in
2023-06-19 08:32:01.126 26298-26298 GraphicsEnvironment<br />
com.example.check_in                 V  ANGLE Developer option for
'com.example.check_in' set to: 'default' 2023-06-19 08:32:01.126
26298-26298 GraphicsEnvironment     com.example.check_in<br />
V  ANGLE GameManagerService for com.example.check_in: false 2023-06-19
08:32:01.126 26298-26298 GraphicsEnvironment     com.example.check_in
V  App is not on the allowlist for updatable production driver.
2023-06-19 08:32:01.128 26298-26298 NetworkSecurityConfig<br />
com.example.check_in                 D  No Network Security Config
specified, using platform default 2023-06-19 08:32:01.133 26298-26298
NetworkSecurityConfig   com.example.check_in                 D  No
Network Security Config specified, using platform default 2023-06-19
08:32:01.139 26298-26330 OpenGLRenderer          com.example.check_in
D  RenderThread::requireGlContext() 2023-06-19 08:32:01.140
26298-26298 AndroidRuntime          com.example.check_in<br />
D  Shutting down VM 2023-06-19 08:32:01.140 26298-26298 AndroidRuntime
com.example.check_in                 E  FATAL EXCEPTION: main
Process: com.example.check_in, PID: 26298
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{com.example.check_in/com.example.check_in.SmsTracker}:
java.lang.InstantiationException:
java.lang.Class&lt;com.example.check_in.SmsTracker&gt; has no zero argument
constructor
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4051)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4315)
at
android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101)
at
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loopOnce(Looper.java:226)
at android.os.Looper.loop(Looper.java:313)
at android.app.ActivityThread.main(ActivityThread.java:8747)
at java.lang.reflect.Method.invoke(Native Method)
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067)
Caused by: java.lang.InstantiationException:
java.lang.Class&lt;com.example.check_in.SmsTracker&gt; has no zero argument
constructor
at java.lang.Class.newInstance(Native Method)
at
android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at
androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1328)
at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4038)
at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4315) 
at
android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:101) 
at
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at
android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:2574) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loopOnce(Looper.java:226) 
at android.os.Looper.loop(Looper.java:313) 
at android.app.ActivityThread.main(ActivityThread.java:8747) 
at java.lang.reflect.Method.invoke(Native Method) 
at
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:571) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1067) 
2023-06-19 08:32:01.140 26298-26330 AdrenoGLES-0<br />
com.example.check_in                 I  QUALCOMM build<br />
: 9ab6a2b2d8, Ifc633ebcef
Build Date                       : 01/24/23

已尝试的解决方案

我添加了无参构造函数,但问题仍未解决:

class SmsTracker(context: Context) : ContextWrapper(context) {
    constructor() : this(ApplicationContextProvider.context)
}

解决方法

核心问题

你误将SmsTracker作为Activity在AndroidManifest.xml中注册了,但它本质是一个工具类,既没有继承Activity,也不应该被系统当作Activity启动。Android系统启动Activity时必须调用无参构造函数,而你的类设计不符合Activity的要求,即使添加无参构造也无法解决根本问题。

修复步骤

  1. 移除Manifest中的错误注册:打开AndroidManifest.xml,找到SmsTracker对应的<activity>注册项并删除,它不是Activity,不需要注册。

  2. 修改SmsTracker类设计:不需要继承ContextWrapper,直接持有Context引用即可,普通工具类没必要包装Context:

class SmsTracker(private val context: Context) {
    private val contentResolver: ContentResolver = context.contentResolver
    private var cursor: Cursor? = null
    // 在这里添加你的业务方法
}
  1. 在真正的Activity中实例化:比如在MainActivity中创建SmsTracker实例,传入Activity的Context:
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        
        val smsTracker = SmsTracker(this)
        // 调用smsTracker的相关方法
    }
}

为什么添加无参构造没用?

即使你添加了无参构造,SmsTracker也没有继承Activity,系统无法将它作为Activity启动;另外ApplicationContextProvider.context在应用启动初期可能还未初始化,会导致空指针异常,进一步引发崩溃。


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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.18 15:37:02