NavHostFragment未解析(AndroidX)

时间:2018-05-23 16:52:19

标签: android android-architecture-components android-navigation android-jetpack

出于某种原因,我总是面临以下错误:

     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

我正在关注Guide on Android Developers但是在启动应用程序后立即出现上述错误。

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(app)

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导入它。在Build your first App with Jetpack他们使用了implementation 'androidx.navigation:navigation-fragment:' + rootProject.navigationVersion但我无法确定他们使用的是哪个版本,所以我读了Architecture Component Release Notes,其中应该使用AndroidX '2.0.0-alpha1'。不幸的是,这无法解决。

0 个答案:

没有答案