Home > Net >  Android Navigation Components Error: 'bad operand types for binary operator' in generated
Android Navigation Components Error: 'bad operand types for binary operator' in generated

Time:03-17

I am working on an app that incorporates Android Navigation Components. A generated build file MyFragmentDirections.javais 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'
}

CodePudding user response:

As per the documentation, the only correct argType for an integer is app:argType="integer" - that will make a int in Java code or a kotlin.Int in Kotlin code.

  • Related