本文主要是介绍Could not inflate Behavior subclass,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
为了处理CoordinatorLayout + AppBarLayout + ViewPager滑动出现回弹和卡顿的现象,自定义的一个AppBarLayoutBehavior,但在运行过程中会出现java.lang.RuntimeException: Could not inflate Behavior subclass
刚开始的布局如下:
<android.support.design.widget.CoordinatorLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"><android.support.design.widget.AppBarLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:background="@color/color_F8F8F8"app:layout_behavior=".ui.widget.AppBarLayoutBehavior"app:elevation="0dp">..........</android.support.design.widget.AppBarLayout>..........
</android.support.design.widget.CoordinatorLayout>
问题的原因就是直接引用了这个路径
static Behavior parseBehavior(Context context, AttributeSet attrs, String name) {if (TextUtils.isEmpty(name)) {return null;}final String fullName;if (name.startsWith(".")) {// Relative to the app package. Prepend the app package name.fullName = context.getPackageName() + name;} else if (name.indexOf('.') >= 0) {// Fully qualified package name.fullName = name;} else {// Assume stock behavior in this package (if we have one)fullName = !TextUtils.isEmpty(WIDGET_PACKAGE_NAME)? (WIDGET_PACKAGE_NAME + '.' + name): name;}try {Map<String, Constructor<Behavior>> constructors = sConstructors.get();if (constructors == null) {constructors = new HashMap<>();sConstructors.set(constructors);}Constructor<Behavior> c = constructors.get(fullName);if (c == null) {final Class<Behavior> clazz = (Class<Behavior>) context.getClassLoader().loadClass(fullName);c = clazz.getConstructor(CONSTRUCTOR_PARAMS);c.setAccessible(true);constructors.put(fullName, c);}return c.newInstance(context, attrs);} catch (Exception e) {throw new RuntimeException("Could not inflate Behavior subclass " + fullName, e);}}
这个是获取文件路径的逻辑,如果以.开头将会取packageName + 名称。如果拼接完成后不等于这个文件的绝对路径将会抛出异常,所以要使用behavior的完整路径。
更加规范的做法是配置到string.xml文件中
这篇关于Could not inflate Behavior subclass的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!