Android Studio 无法识别方向类

问题描述 投票:0回答:1

r有一段时间,我已经习惯了这样一个事实:有时 IDE (Android Studio Koala 2024.1.1) 会用红色标记导航方向,但仍会编译应用程序,运行良好。示例:

enter image description here

请注意,这有一个不方便之处,您习惯于忽略红色标记,需要编译才能找出真正的编码错误。

今天,我添加了一个新的导航链接,这次生成了运行时异常:

enter image description here

此处的导航操作是在房间数据库查询后通过单击 Snackbar 操作生成的:

enter image description here再次,尽管有红色标记,代码仍编译成功,但现在生成运行时异常。 这里是代码:

private fun actionEditOnClickListener(): View.OnClickListener = View.OnClickListener{ view ->
    val action: FragmentAddMotDirections.AddMotToEditMot = FragmentAddMotDirections.addMotToEditMot()
    val viewModel = ViewModelProvider(requireActivity())[SharedViewModel::class.java]
    viewModel.orthoAlt(motEnCours, defMotAEditer)
    viewModel.getOrthoAlt().observe(viewLifecycleOwner) { ort ->
        ort?.let {
            action.mot = motEnCours
            if (it.isNotEmpty()){
                action.orthoAlt = "= " + it.joinToString ("  ")
            }
            action.defId = defMotAEditer
            Navigation.findNavController(view).navigate(action)
        }
    }
}

有人可以告诉我问题是否与导航类生成有关,或者小吃栏代码中是否有其他不适合的内容?

我知道有很多帖子询问导航类生成,我尝试了大多数建议的答案但没有成功。

这是项目gradle:

buildscript {
    dependencies {
        classpath("androidx.navigation:navigation-safe-args-gradle-plugin:2.7.7")
    }
}

plugins {
    id("com.android.application") version "8.5.1" apply false
    id("org.jetbrains.kotlin.android") version "1.9.21" apply false
    id("com.google.devtools.ksp") version "1.9.21-1.0.15" apply false
}

应用程序等级:

plugins {
    id("com.android.application")
    id("org.jetbrains.kotlin.android")
    id("com.google.devtools.ksp")
    id ("androidx.navigation.safeargs")
}
ksp {
    arg("room.schemaLocation", "$projectDir/schemas")
}

android {
    lint {
        baseline = file("lint-baseline.xml")
    }

    buildFeatures {
        viewBinding = true
    }
    namespace = "com.simonwintz.millemots"
    compileSdk = 34

    defaultConfig {
        applicationId = "com.simonwintz.millemots"
        minSdk = 26
        //noinspection OldTargetApi
        targetSdk = 34
        versionCode = 15
        versionName = "2.5"

        testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
    }

    buildTypes {
        release {
            isMinifyEnabled = false
            proguardFiles(
                getDefaultProguardFile("proguard-android-optimize.txt"),
                "proguard-rules.pro"
            )
        }
    }
    compileOptions {
        sourceCompatibility = JavaVersion.VERSION_17
        targetCompatibility = JavaVersion.VERSION_17
    }
    kotlinOptions {
        jvmTarget = "17"
    }
}

dependencies {

    implementation("androidx.core:core-ktx:1.13.1")
    implementation("androidx.appcompat:appcompat:1.7.0")
    implementation("com.google.android.material:material:1.12.0")
    implementation("androidx.constraintlayout:constraintlayout:2.1.4")
    implementation ("androidx.navigation:navigation-fragment-ktx:2.7.7")
    implementation ("androidx.navigation:navigation-ui-ktx:2.7.7")
    implementation("androidx.legacy:legacy-support-v4:1.0.0")
    testImplementation("junit:junit:4.13.2")
    implementation ("androidx.room:room-runtime:2.6.1")
    implementation ("androidx.fragment:fragment-ktx:1.8.1")
    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.8.1")
    implementation ("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.8.1")
    ksp ("androidx.room:room-compiler:2.6.1")
    androidTestImplementation("androidx.test.ext:junit:1.2.1")
    androidTestImplementation("androidx.test.espresso:espresso-core:3.6.1")
}

这里是导航图:

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/navigation_graph"
    app:startDestination="@id/fragmentBaseMots">

    <fragment
        android:id="@+id/fragmentBaseMots"
        android:name="com.simonwintz.millemots.FragmentBaseMots"
        android:label="fragment_base_mots"
        tools:layout="@layout/fragment_base_mots" >
        <action
            android:id="@+id/baseMotsToJeu"
            app:destination="@id/fragmentJeu" />
        <action
            android:id="@+id/baseMotsToRefill"
            app:destination="@id/fragmentRefill" />
        <action
            android:id="@+id/baseMotsToPref"
            app:destination="@id/fragmentPref" />
        <action
            android:id="@+id/baseMotsToAddMot"
            app:destination="@id/fragmentAddMot" />
        <action
            android:id="@+id/baseMotsToEditMot"
            app:destination="@id/fragmentEditMot" />
        <action
            android:id="@+id/baseMotsToShare"
            app:destination="@id/fragmentShare" />
        <action
            android:id="@+id/baseMotsToFiltres"
            app:destination="@id/fragmentFiltres" />

    </fragment>
    <fragment
        android:id="@+id/fragmentJeu"
        android:name="com.simonwintz.millemots.FragmentJeu"
        android:label="fragment_jeu"
        tools:layout="@layout/fragment_jeu" >
        <argument
            android:name="totalMots"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="totalCoups"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="totalActifs"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="tauxMoyen"
            app:argType="string"
            android:defaultValue='""' />
    </fragment>
    <fragment
        android:id="@+id/fragmentRefill"
        android:name="com.simonwintz.millemots.FragmentRefill"
        android:label="FragmentRefill"
        tools:layout="@layout/fragment_refill">
        <argument
            android:name="message"
            app:argType="string"
            android:defaultValue="No message!" />
        <argument
            android:name="tauxErreur"
            app:argType="float"
            android:defaultValue="0.0" />
    </fragment>
    <fragment
        android:id="@+id/fragmentPref"
        android:name="com.simonwintz.millemots.FragmentPref"
        android:label="fragment_pref"
        tools:layout="@layout/fragment_pref">
        <argument
            android:name="Message"
            app:argType="string"
            android:defaultValue='"No message!"' />
        <argument
            android:name="totalBase"
            app:argType="integer"
            android:defaultValue="0" />
    </fragment>
    <fragment
        android:id="@+id/fragmentHelp"
        android:name="com.simonwintz.millemots.FragmentHelp"
        tools:layout="@layout/fragment_help"
        android:label="FragmentHelp" >
        <argument
            android:name="dispSwitch"
            app:argType="boolean"
            android:defaultValue="true" />
    </fragment>
    <action android:id="@+id/moveToHelp"
        app:destination="@+id/fragmentHelp"/>
    <fragment
        android:id="@+id/fragmentAddMot"
        android:name="com.simonwintz.millemots.FragmentAddMot"
        android:label="FragmentAddMot"
        tools:layout="@layout/fragment_add_mot" >
        <argument
            android:name="mot"
            app:argType="string" />
        <action
            android:id="@+id/addMotToEditMot"
            app:destination="@id/fragmentEditMot" />
    </fragment>
    <fragment
        android:id="@+id/fragmentEditMot"
        android:name="com.simonwintz.millemots.FragmentEditMot"
        android:label="FragmentEditMot"
        tools:layout="@layout/fragment_edit_mot" >
        <argument
            android:name="mot"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="orthoAlt"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="defId"
            app:argType="long"
            android:defaultValue="0L" />
    </fragment>
    <fragment
        android:id="@+id/fragmentShare"
        android:name="com.simonwintz.millemots.FragmentShare"
        android:label="FragmentShare"
        tools:layout="@layout/fragment_share" >
        <argument
            android:name="message"
            app:argType="string"
            android:defaultValue='"No message!"' />
    </fragment>
    <fragment
        android:id="@+id/fragmentFiltres"
        android:name="com.simonwintz.millemots.FragmentFiltres"
        android:label="FragmentFiltres"
        tools:layout="@layout/fragment_filtres" >
        <action
            android:id="@+id/filtresToJeu"
            app:destination="@id/fragmentJeu" />
        <argument
            android:name="totalMots"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="totalCoups"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="totalActifs"
            app:argType="string"
            android:defaultValue='""' />
        <argument
            android:name="tauxMoyen"
            app:argType="string"
            android:defaultValue='""' />
        <action
            android:id="@+id/filtresToAddMot"
            app:destination="@id/fragmentAddMot" />
    </fragment>
</navigation>
kotlin android-studio navigation uinavigationcontroller android-safe-args
1个回答
0
投票

你使用 kotlin 所以你的插件是 id ("androidx.navigation.safeargs.kotlin") 不是 id(“androidx.navigation.safeargs”)

© www.soinside.com 2019 - 2024. All rights reserved.