简体   繁体   中英

Unable to instantiate fragment make sure class name exists

android.app.Fragment$InstantiationException: Unable to instantiate fragment: make sure class name exists, is public, and has a empty constructor that is public.

I realize there are many questions posted regarding the same error, but none have helped me so far. I am learning from a book, and have actually run the exact(99% sure) code before. After erasing it all and rewriting it for review, the error came up.

package com.paad.todolist;

import android.app.ListFragment;
import android.os.Bundle;

public class ToDoListFragment extends ListFragment{

    public static ToDoListFragment newInstance(int index) {
        ToDoListFragment f = new ToDoListFragment();

           Bundle args = new Bundle();
           args.putInt("index", index);
           f.setArguments(args);

           return f;
        }
    }

LogCat:

06-16 03:04:14.091: E/AndroidRuntime(836): FATAL EXCEPTION: main 06-16 03:04:14.091: E/AndroidRuntime(836): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.paad.todolist/com.paad.todolist.ToDoListActivity}: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.ActivityThread.access$600(ActivityThread.java:123) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.os.Handler.dispatchMessage(Handler.java:99) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.os.Looper.loop(Looper.java:137) 06-16 03:04:14.091: E/AndroidRunt ime(836): at android.app.ActivityThread.main(ActivityThread.java:4424) 06-16 03:04:14.091: E/AndroidRuntime(836): at java.lang.reflect.Method.invokeNative(Native Method) 06-16 03:04:14.091: E/AndroidRuntime(836): at java.lang.reflect.Method.invoke(Method.java:511) 06-16 03:04:14.091: E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 06-16 03:04:14.091: E/AndroidRuntime(836): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 06-16 03:04:14.091: E/AndroidRuntime(836): at dalvik.system.NativeStart.main(Native Method) 06-16 03:04:14.091: E/AndroidRuntime(836): Caused by: android.view.InflateException: Binary XML file line #13: Error inflating class fragment 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.rInflate(LayoutInflater.java:739) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.inflate(LayoutInflater.java:489) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-16 03:04:14.091: E/AndroidRuntime(836): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Activity.setContentView(Activity.java:1835) 06-16 03:04:14.091: E/AndroidRuntime(836): at com.paad.todolist.ToDoListActivity.onCreate(ToDoListActivity.java:18) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Activity.performCreate(Activity.java:4466) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 06-16 03:04:14.091: E/AndroidRun time(836): ... 11 more 06-16 03:04:14.091: E/AndroidRuntime(836): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment com.paad.todoList.ToDoListFragment: make sure class name exists, is public, and has an empty constructor that is public 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Fragment.instantiate(Fragment.java:585) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Fragment.instantiate(Fragment.java:549) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Activity.onCreateView(Activity.java:4235) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:673) 06-16 03:04:14.091: E/AndroidRuntime(836): ... 21 more 06-16 03:04:14.091: E/AndroidRuntime(836): Caused by: java.lang.ClassNotFoundException: com.paad.todoList.ToDoListFragment 06-16 03:04:14.091: E/AndroidRuntime(836): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-16 03:04:14.091: E/AndroidRuntime(836): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 06-16 03:04:14.091: E/AndroidRuntime(836): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 06-16 03:04:14.091: E/AndroidRuntime(836): at android.app.Fragment.instantiate(Fragment.java:571) 06-16 03:04:14.091: E/AndroidRuntime(836): ... 24 more

Found that, you logcat says everything

 Caused by: java.lang.ClassNotFoundException: com.paad.todoList.ToDoListFragment

Your package name is not correct. use com.paad.todolist instead of com.paad.todoList

This post reminds me the TV Show dialog, Sherlock : You see but you do not observe :) :)

TL;DR:

If you recently migrated files or did any form of copy and paste, check that all your files have an updated package name.

Full story:

I created a new module and then copied and pasted the entire java and res directories from another module after which I deleted the old module. To avoid any possible conflicts, I checked all the Kotlin/Java files and renamed the package name to reflect the name of the new package that the files now belonged to. Here's what I mean:

From:

com.myapp.oldmodule.presentation.thisfile

To:

com.myapp.newmodule.presentation.thisfile

Normally, this should have resolved any potential issue. My mistake however was that I forgot to update the navigation-graph file as well ; It still had references that contained the old/deleted module.

There are many tutorials/projects from Google and others. I think your main problem now is that the caller cannot create a new instance of your Fragment . One good Google webpage is @ Fragments .

Code snippet from the webpage:

public static class DetailsFragment extends Fragment {
    /**
     * Create a new instance of DetailsFragment, initialized to
     * show the text at 'index'.
     */
    public static DetailsFragment newInstance(int index) {
        DetailsFragment f = new DetailsFragment();

        // Supply index input as an argument.
        Bundle args = new Bundle();
        args.putInt("index", index);
        f.setArguments(args);

        return f;
    }

Notes:

  • Instead of extends Fragment , replace with extends ListFragment in your case.
  • Notice the method newInstance (). This replaces the new keyword.

For me this error occurs after moving the XxxFragment.kt file using the refactoring feature of AndroidStudio.

It is important to check every place where the Fragment is referenced to find the cause. Some locations are not covered by refactoring. You can find all places while using the global search (Press Shift twice) for XxxFragment .

In my case two places where not refactored automatically:

  • android:name of the fragment in the nav_graph.xml
  • tools:context import in the fragment_xxx.xml file itself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM