Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment

Error

Unable to instantiate fragment androidx.navigation.fragment.NavHostFragment: make sure class name exists, is public, and has an empty constructor that is public.

ActivityLayout

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

Activity

class HomeActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {

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

Fragment

class MovieListFragment : Fragment() {
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_movie_list, container, false)
    }
}

Dependency Used:

implementation('android.arch.navigation:navigation-fragment-ktx:1.0.0-alpha07') {
    exclude group: "com.android.support"
}
implementation('android.arch.navigation:navigation-ui-ktx:1.0.0-alpha07') {
    exclude group: "com.android.support"
}
like image 720
Rajesh Khadka Avatar asked Nov 05 '18 11:11

Rajesh Khadka


1 Answers

If its happening only on the proguarded version, you might have forgotten to add the proguard rule. Add the below rule in your proguard file

-keepnames class androidx.navigation.fragment.NavHostFragment
like image 105
sanket vetkoli Avatar answered Nov 03 '22 01:11

sanket vetkoli