Android 导航组件错误:“二元运算符的操作数类型错误”在生成的文件中
我正在开发一个包含 Android 导航组件的应用程序。生成的构建文件 MyFragmentDirections.java
正在阻止构建应用程序,该文件包含许多类似于以下内容的错误:
error: bad operand types for binary operator '=='
if (mainItemId == null) {
^
first type: int
second type: <null>
...
error: int cannot be dereferenced
if (getMainItemId() != null ? !getMainItemId().equals(that.getMainItemId()) : that.getMainItemId() != null) {
我怀疑问题出在我定义的导航参数中:
<argument
android:name="mainItemId"
app:argType="int"/>
我使用了 < code>int 而不是 Int
,因为我在 Android Developer 上看到的示例使用 string
而不是 String
。
当我引用导航参数时,更改为 Int
会导致代码中其他地方出现一组不同的错误:
Type mismatch: inferred type is Int but kotlin.Int was expected
Cannot access class 'Int'. Check your module classpath for missing or conflicting dependencies
任何人都可以解释为什么会发生这种情况并提供解决方案吗?
我的 Gradle 构建文件(模块)的相关部分如下:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin' // edited see comments
}
...
dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
}
I am working on an app that incorporates Android Navigation Components. A generated build file MyFragmentDirections.java
is preventing the app from being built, this file contains numerous errors similar to the ones below:
error: bad operand types for binary operator '=='
if (mainItemId == null) {
^
first type: int
second type: <null>
...
error: int cannot be dereferenced
if (getMainItemId() != null ? !getMainItemId().equals(that.getMainItemId()) : that.getMainItemId() != null) {
I suspect the problem is in a Navigation Argument I have defined:
<argument
android:name="mainItemId"
app:argType="int"/>
I have used int
instead of Int
because examples I have seen on Android Developer use string
instead of String
.
Changing to Int
results in a different set of errors elsewhere in my code when I reference the navigation argument:
Type mismatch: inferred type is Int but kotlin.Int was expected
Cannot access class 'Int'. Check your module classpath for missing or conflicting dependencies
Can anyone explain why this happening and provide a solution?
The relevant portion of my Gradle build file (Module) is below:
plugins {
id 'com.android.application'
id 'org.jetbrains.kotlin.android'
id 'kotlin-android-extensions'
id 'androidx.navigation.safeargs.kotlin' // edited see comments
}
...
dependencies {
implementation 'androidx.navigation:navigation-fragment-ktx:2.4.1'
implementation 'androidx.navigation:navigation-ui-ktx:2.4.1'
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据文档,唯一 整数的正确
argType
是app:argType="integer"
- 这将在 Java 代码中生成int
或Kotlin 代码中的kotlin.Int
。As per the documentation, the only correct
argType
for an integer isapp:argType="integer"
- that will make aint
in Java code or akotlin.Int
in Kotlin code.