Unable to instantiate fragment make sure class name....

本文探讨了在使用Viewpager+Fragment时,遇到的无法启动Activity的异常问题,具体为RuntimeException和Fragment$InstantiationException。通过分析发现,自定义Fragment仅定义了带参数的构造方法时,需要确保添加参数为空的构造函数。本文提供了解决方法并解释了其背后的原因。

最近使用Viewpager + Fragment来做应用,经常会遇到如下错误

E/UncaughtException-main(13661): java.lang.RuntimeException: Unable to start activity ComponentInfo{......}: android.app.Fragment$InstantiationException: Unable to instantiate fragment xxxxxx...: make sure class name exists, is public, and has an empty constructor that is public

解决办法:

自定义的fragment 如果只定义了带参数的构造方法,那么要把参数为空的构造函数一并写上。原因待进一步研究,知道的朋友不吝赐教!




/** * Create a new instance of a Fragment with the given class name. This uses * {@link #loadFragmentClass(ClassLoader, String)} and the empty * constructor of the resulting Class by default. * * @param classLoader The default classloader to use for instantiation * @param className The class name of the fragment to instantiate. * @return Returns a new fragment instance. * @throws Fragment.InstantiationException If there is a failure in instantiating * the given fragment class. This is a runtime exception; it is not * normally expected to happen. */ @NonNull public Fragment instantiate(@NonNull ClassLoader classLoader, @NonNull String className) { try { Class<? extends Fragment> cls = loadFragmentClass(classLoader, className); return cls.getConstructor().newInstance(); } catch (java.lang.InstantiationException e) { throw new Fragment.InstantiationException("Unable to instantiate fragment " + className + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new Fragment.InstantiationException("Unable to instantiate fragment " + className + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (NoSuchMethodException e) { throw new Fragment.InstantiationException("Unable to instantiate fragment " + className + ": could not find Fragment constructor", e); } catch (InvocationTargetException e) { throw new Fragment.InstantiationException("Unable to instantiate fragment " + className + ": calling Fragment constructor caused an exception", e); } } } 翻译并解析
最新发布
09-29
/** * Create a new instance of a Fragment with the given class name. This is * the same as calling its empty constructor, setting the {@link ClassLoader} on the * supplied arguments, then calling {@link #setArguments(Bundle)}. * * @param context The calling context being used to instantiate the fragment. * This is currently just used to get its ClassLoader. * @param fname The class name of the fragment to instantiate. * @param args Bundle of arguments to supply to the fragment, which it * can retrieve with {@link #getArguments()}. May be null. * @return Returns a new fragment instance. * @throws InstantiationException If there is a failure in instantiating * the given fragment class. This is a runtime exception; it is not * normally expected to happen. * @deprecated Use {@link FragmentManager#getFragmentFactory()} and * {@link FragmentFactory#instantiate(ClassLoader, String)}, manually calling * {@link #setArguments(Bundle)} on the returned Fragment. */ @Deprecated @NonNull public static Fragment instantiate(@NonNull Context context, @NonNull String fname, @Nullable Bundle args) { try { Class<? extends Fragment> clazz = FragmentFactory.loadFragmentClass( context.getClassLoader(), fname); Fragment f = clazz.getConstructor().newInstance(); if (args != null) { args.setClassLoader(f.getClass().getClassLoader()); f.setArguments(args); } return f; } catch (java.lang.InstantiationException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (IllegalAccessException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": make sure class name exists, is public, and has an" + " empty constructor that is public", e); } catch (NoSuchMethodException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": could not find Fragment constructor", e); } catch (InvocationTargetException e) { throw new InstantiationException("Unable to instantiate fragment " + fname + ": calling Fragment constructor caused an exception", e); } }解析
09-29
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值