7

出于某种原因,我总是遇到以下错误:

     Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public

我一直在关注 Android 开发人员指南,但在启动应用程序后立即出现上述错误。

nav_graph.xml

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

<fragment
    android:id="@+id/mainFragment"
    android:name="projects.ferrari.rene.sken.ui.main.MainFragment"
    android:label="main_fragment"
    tools:layout="@layout/main_fragment" >
    <action
        android:id="@+id/action_mainFragment_to_takeImageFragment"
        app:destination="@id/takeImageFragment" />
</fragment>
<fragment
    android:id="@+id/takeImageFragment"
    android:name="projects.ferrari.rene.sken.ui.takeimage.TakeImageFragment"
    android:label="take_image_fragment"
    tools:layout="@layout/take_image_fragment" />

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <fragment
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/my_nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        app:navGraph="@navigation/nav_graph"
        app:defaultNavHost="true"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

    package projects.ferrari.rene.sken

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.navigation.findNavController

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.main_activity)
    }

    override fun onSupportNavigateUp()
            = findNavController(R.id.my_nav_host_fragment).navigateUp()
}

build.gradle(应用程序)

apply plugin: 'com.android.application'

apply plugin: 'kotlin-android'

apply plugin: 'kotlin-android-extensions'

android {
    compileSdkVersion 'android-P'
    defaultConfig {
        applicationId "projects.ferrari.rene.sken"
        minSdkVersion 21
        targetSdkVersion 28
        versionCode 1
        versionName "1.0"
        testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    def anko_version = "0.10.5"
    def nav_version = "1.0.0-alpha01"

    implementation "android.arch.navigation:navigation-fragment-ktx:$nav_version"
    implementation "android.arch.navigation:navigation-ui-ktx:$nav_version"
    implementation "org.jetbrains.anko:anko-commons:$anko_version"
    implementation 'com.google.android.material:material:1.0.0-alpha1'
    implementation 'com.google.android.gms:play-services-vision:15.0.2'
    implementation 'androidx.core:core-ktx:1.0.0-alpha1'
    implementation 'com.github.pqpo:SmartCropper:v1.1.3@aar'
    implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
    implementation 'androidx.appcompat:appcompat:1.0.0-alpha1'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.0'
    implementation 'androidx.lifecycle:lifecycle-extensions:2.0.0-alpha1'
    implementation 'androidx.legacy:legacy-support-v4:1.0.0-alpha1'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha2'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha2'
}

基本上我正在使用KotlinAndroidX并针对Android P。我真的不知道问题出在哪里。

编辑导入可能有问题。对于导航,我不使用AndroidX. 在使用 Jetpack 构建您的第一个应用程序中,他们使用implementation 'androidx.navigation:navigation-fragment:' + rootProject.navigationVersion但我无法弄清楚他们使用的是哪个版本,因此我阅读了架构组件发行说明,其中说'2.0.0-alpha1'应该使用 AndroidX。不幸的是,这无法解决。

4

0 回答 0