android - 为什么Intent中的类有“::class.java”后缀?
问题描述
Kotlin 中的一个意图:
val intent = Intent(this, OtherActivity::class.java)
为什么不能这样:
val intent = Intent(this, OtherActivity)
?
解决方案
该 Intent 构造函数的第二个参数需要您要创建的 Activity的类。虽然只使用类名来获取类会很方便OtherActivity
,但 Java(和 Kotlin)语法不支持这一点。
相反,Java 提供.class
语法 ( OtherActivity.class
),Kotlin::class
为 Kotlin 类提供语法,而::class.java
( OtherActivity::class.java
) 为 Java 类提供,这正是 Intent 构造函数所需要的。
推荐阅读
- html - Why is the width of a website the same even when I change the viewport in Chrome Debug Tools?
- android - kotlin 创建一个 Fragment,出现如下错误:
- hive - 配置单元中的无效函数 regex_extract
- node.js - 在 Node.js 中的单个 URL 上托管 2 个网站
- javascript - 从这三个收音机中制作一个按钮
- r - 转“|” 将值分隔到 r 中的列表中
- c++ - 如何在不同文件之间组织 qml (QT) 代码和实例
- python - 有什么方法可以更快地生成一个矩阵,该矩阵在一行中至少有一个非零元素?
- c# - 将值从第一个 datagridview 传递到另一个 datagridview
- powershell - 使用 Powershell 检查 RDP 登录成功