Q小文Q
Q小文Q
  • 发布:2024-08-26 18:00
  • 更新:2024-08-27 18:53
  • 阅读:166

【报Bug】uni-app项目开发uts插件运行编译或提交云打包自定义基座,一直报错'onActivityCreated' overrides nothing

分类:uni-app

产品分类: uniapp/App

PC开发环境操作系统: Windows

PC开发环境操作系统版本号: Windows 11

HBuilderX类型: Alpha

HBuilderX版本号: 4.26

手机系统: Android

手机系统版本号: Android 14

手机厂商: OPPO

手机机型: 一加ACE2

页面类型: vue

vue版本: vue2

打包方式: 云端

项目创建方式: HBuilderX

示例代码:

import Application from 'android.app.Application';
import Bundle from 'android.os.Bundle';
import Activity from 'android.app.Activity';
import ActivityLifecycleCallbacks from 'android.app.Application.ActivityLifecycleCallbacks';
// class MyLifecycleCallbacks extends ActivityLifecycleCallbacks {
// override onActivityCreated(a?: Activity, b?: Bundle):void {
// // 处理Activity创建事件
// }
// }
class MyLifecycleCallbacks implements ActivityLifecycleCallbacks {
override onActivityCreated(a?: Activity, b?: Bundle):void {
// 处理Activity创建事件
}
}
export class YDAppHookProxy implements UTSAndroidHookProxy {
override onCreate(application: Application) {
application.registerActivityLifecycleCallbacks(new MyLifecycleCallbacks());
}
}

操作步骤:
  1. uni-app项目新建uts插件
  2. 自定义类实现ActivityLifecycleCallbacks接口(详见代码示例)
  3. 在 UTSAndroidHookProxy 的onCreate中application.registerActivityLifecycleCallbacks(new MyLifecycleCallbacks());(详见代码示例)
  4. 本地运行或云打包自定义基座时,一直提示报错:
    error: Class 'MyLifecycleCallbacks' is not abstract and does not implement abstract member public abstract fun onActivityCreated(p0: Activity, p1: Bundle?): Unit defined in android.app.Application.ActivityLifecycleCallbacks‌
    error: 'onActivityCreated' overrides nothing‌

预期结果:
  1. 编译运行、云打包不报错
  2. 运行打开App时,onActivityCreated应正常执行

实际结果:

运行编译报错,云打包报错

bug描述:

uni-app项目开发uts插件application.registerActivityLifecycleCallbacks,运行编译或提交云打包自定义基座,一直报错:

error: Class 'MyLifecycleCallbacks' is not abstract and does not implement abstract member public abstract fun onActivityCreated(p0: Activity, p1: Bundle?): Unit defined in android.app.Application.ActivityLifecycleCallbacks‌

error: 'onActivityCreated' overrides nothing‌

2024-08-26 18:00 负责人:DCloud_Android_MHF 分享
已邀请:
Q小文Q

Q小文Q (作者)

已解决,感谢@DCloud_Android_DQQ管理员。参考kotlin的空安全(override onActivityCreated(a: Activity, b: Bundle|null):void),使用以下代码就正常了。

class MyLifecycleCallbacks implements ActivityLifecycleCallbacks {
override onActivityCreated(a: Activity, b: Bundle|null):void {
console.log("onActivityCreated",a)
}

override onActivityStarted(a: Activity):void {
console.log("onActivityStarted",a)
}

override onActivityResumed(a: Activity):void {
console.log("onActivityResumed",a)
}

override onActivityPaused(a: Activity):void {
console.log("onActivityPaused",a)
}

override onActivityStopped(a: Activity):void {
console.log("onActivityStopped",a)
}

override onActivitySaveInstanceState(a: Activity, b: Bundle):void {
console.log("onActivitySaveInstanceState",a)
}

override onActivityDestroyed(a: Activity):void {
console.log("onActivityDestroyed",a)
}

}

export class AppHookProxy implements UTSAndroidHookProxy {
override onCreate(application: Application) {
//当前应用是否 取得用户同意隐私协议
Log.d("AppHookProxy", "AppHookProxy--onCreate---")
// 初始化快捷方式
initShortCut()
if(UTSAndroid.isPrivacyAgree()) {
//onCreate 初始化三方SDK
Log.d("AppHookProxy", "AppHookProxy--onCreate---isPrivacyAgree")
}

application.registerActivityLifecycleCallbacks(new MyLifecycleCallbacks());  

}
}

要回复问题请先登录注册