如何替换 Java 20 中已弃用的 URL 构造函数:Deprecated constructorURL.<init>(String)is invoked

该文章已生成可运行项目,

一.报错译文

Deprecated constructor已弃用的构造函数URL.<init>(String)is invoked

二.解决办法

  官方说明文档:URL (Java SE21 & JDK 21),建议使用URI.toURL()

三.代码示例

//java 20弃用 new URL的所有构造函数
//报错代码
//ImageIcon imageIcon = new ImageIcon((new URL("https://codechrono.cn/wechatMiniKeyPng.png")), "wechatMiniPng");
//第一次尝试
//ImageIcon imageIcon = new ImageIcon(new URL("https","codechrono.cn",80,"/wechatMiniKeyPng.png"), "wechatMiniPng");
//第二次尝试,成功代码 URI.toURL()
ImageIcon imageIcon = new ImageIcon(((new URI("https://codechrono.cn/wechatMiniKeyPng.png")).toURL()), "wechatMiniPng");

本文章已经生成可运行项目
你遇到的这个警告: > **The signature '(options: RouterOptions): Promise<void>' of 'router.pushUrl' is deprecated.** 是因为在较新的 HarmonyOS SDK(尤其是 API 10+)中,`router.pushUrl(options)` 的原始写法已经被**用**(deprecated),官方推荐使用新的 `router.pushUrl({ url, params })` 形式,并且需要从 `@ohos.router` 中导入新的类型。 --- ## ✅ 解决方案 你需要将原来的写法: ```ts router.pushUrl({ url: 'pages/ChatPage', params: { chatItem: JSON.stringify(item) } }).catch((error: Error) => { console.error('页面跳转失败:', error.message); }); ``` 替换为 **新版本推荐写法**: ### ✅ 新写法(推荐) ```ts import router from '@ohos.router'; // 跳转到 ChatPage 并传递数据 router.pushUrl({ url: 'pages/ChatPage', params: { chatItem: JSON.stringify(item) } } as router.RouterOptions).catch((error: Error) => { console.error('页面跳转失败:', error.message); }); ``` 或者更严格地使用: ```ts import router from '@ohos.router'; router.pushUrl({ url: 'pages/ChatPage', params: { chatItem: JSON.stringify(item) } } satisfies router.PushUrlOptions).catch((err: Error) => { console.error('跳转失败', err.message); }); ``` --- ## 🔍 更详细的解释 - 在新版本中: - `PushUrlOptions` 是用于 `pushUrl()` 的参数类型。 - `RouterOptions` 已被标记为过时。 - 所以你需要显式地告诉 TypeScript 使用的是 `PushUrlOptions` 类型。 --- ## 🧪 示例:完整点击跳转逻辑 ```ts // MassageList.ets import router from '@ohos.router'; interface MessageItem { id: number; name: string; content: string; time: string; avatar: Resource; unreadCount: number; } @Entry @Component export struct MassageList { @State private messages: MessageItem[] = [ { id: 1, name: '张三', content: '你好,最近怎么样?', time: '14:30', avatar: $r('app.media.avatar1'), unreadCount: 2 } ]; build() { List({ space: 10 }) { ForEach(this.messages, (item: MessageItem) => { ListItem() { Row() { Image(item.avatar) .width(50) .height(50) .borderRadius(25) Column({ space: 5 }) { Text(item.name).fontSize(16).fontWeight(FontWeight.Bold) Text(item.content) .fontSize(14) .fontColor('#666') .maxLines(1) .textOverflow({ type: TextOverflow.Ellipsis }) }.layoutWeight(1) Text(item.time) .fontSize(12) .fontColor('#999') .width(60) .textAlign(TextAlign.End) } .padding({ left: 15, right: 15 }) .onClick(() => { // 使用新版 router.pushUrl router.pushUrl({ url: 'pages/ChatPage', params: { chatItem: JSON.stringify(item) } } satisfies router.PushUrlOptions).catch((err: Error) => { console.error('跳转失败:', err.message); }); }) } }, (item: MessageItem) => item.id.toString()) } } } ``` --- ## ✅ 总结 | 写法 | 是否推荐 | |------|----------| | `router.pushUrl({ url, params })` | ❌ 不推荐(会提示已用) | | `router.pushUrl({ url, params } as router.RouterOptions)` | ⚠️ 可用但不推荐 | | `router.pushUrl({ url, params } satisfies router.PushUrlOptions)` | ✅ 推荐写法 | --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值