自定义查看充气错误

时间:2017-07-09 12:42:11

标签: android inflate-exception

我有一个简单的自定义视图:

public class ScrollDisabledViewPager extends ViewPager {

    public ScrollDisabledViewPager(Context context) {
        super(context);
    }

    public ScrollDisabledViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return false;
    }

}

当我将此View放入我的XML布局时,它不会膨胀:

enter image description here

XML:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    tools:context="com.lol.firebase.ui.SignUpActivity">

    <com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tab_dots"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:tabGravity="center"
        app:tabIndicatorHeight="0dp"
        app:tabBackground="@drawable/tab_indicator_selector" />

    <Button
        android:id="@+id/btn_back"
        android:text="@string/back"
        android:textColor="@color/colorPrimaryLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:drawableLeft="@drawable/ic_back"
        style="@style/Widget.AppCompat.Button.Borderless"
        android:visibility="gone" />

    <Button
        android:id="@+id/btn_next_or_finish"
        android:text="@string/next"
        android:textColor="@color/colorPrimaryLight"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:drawableRight="@drawable/ic_next"
        style="@style/Base.Widget.AppCompat.Button.Borderless" />

</RelativeLayout>

Stacktrace:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lol.firebase/com.lol.firebase.ui.SignUpActivity}: android.view.InflateException: Binary XML file line #9: Error inflating class com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2660)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730)
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:185)
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                                                                          at android.os.Handler.dispatchMessage(Handler.java:111)
                                                                          at android.os.Looper.loop(Looper.java:194)
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5847)
                                                                          at java.lang.reflect.Method.invoke(Native Method)
                                                                          at java.lang.reflect.Method.invoke(Method.java:372)
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
                                                                       Caused by: android.view.InflateException: Binary XML file line #9: Error inflating class com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:763)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:812)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:510)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:420)
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:371)
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
                                                                          at com.lol.firebase.ui.SignUpActivity.onCreate(SignUpActivity.java:29)
                                                                          at android.app.Activity.performCreate(Activity.java:6117)
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613)
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:185) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                          at android.os.Looper.loop(Looper.java:194) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5847) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
                                                                       Caused by: java.lang.ClassNotFoundException: Didn't find class "com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager" on path: DexPathList[[zip file "/data/app/com.lol.firebase-1/base.apk", zip file "/data/app/com.lol.firebase-1/split_lib_dependencies_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_0_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_1_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_2_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_3_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_4_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_5_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_6_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_7_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_8_apk.apk", zip file "/data/app/com.lol.firebase-1/split_lib_slice_9_apk.apk"],nativeLibraryDirectories=[/data/app/com.lol.firebase-1/lib/arm64, /vendor/lib64, /system/lib64]]
                                                                          at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
                                                                          at android.view.LayoutInflater.createView(LayoutInflater.java:577)
                                                                          at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:749)
                                                                          at android.view.LayoutInflater.rInflate(LayoutInflater.java:812) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:510) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:420) 
                                                                          at android.view.LayoutInflater.inflate(LayoutInflater.java:371) 
                                                                          at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292) 
                                                                          at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
                                                                          at com.lol.firebase.ui.SignUpActivity.onCreate(SignUpActivity.java:29) 
                                                                          at android.app.Activity.performCreate(Activity.java:6117) 
                                                                          at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123) 
                                                                          at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2613) 
                                                                          at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2730) 
                                                                          at android.app.ActivityThread.access$800(ActivityThread.java:185) 
                                                                          at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                          at android.os.Handler.dispatchMessage(Handler.java:111) 
                                                                          at android.os.Looper.loop(Looper.java:194) 
                                                                          at android.app.ActivityThread.main(ActivityThread.java:5847) 
                                                                          at java.lang.reflect.Method.invoke(Native Method) 
                                                                          at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                          at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1010) 
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805) 
                                                                        Suppressed: java.lang.ClassNotFoundException: com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager
                                                                          at java.lang.Class.classForName(Native Method)
                                                                          at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
                                                                          at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
                                                                          at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
                                                                                ... 23 more
                                                                       Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available

Stacktrace显示了一个ClassNotFound异常,但我不明白为什么:该类是公共的,我有两个构造函数。

2 个答案:

答案 0 :(得分:0)

这里是相关性错误消息: java.lang.ClassNotFoundException: Didn't find class "com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager" 请确保课程ScrollDisabledViewPager位于包com.lol.firebase.ui.SignUpActivity

但是,我猜你的课程在com.lol.firebase.ui而不是com.lol.firebase.ui.SignUpActivity,如果我的猜测正确,你应该在xml布局中定义com.lol.firebase.ui.ScrollDisabledViewPager

...other xml layout

<com.lol.firebase.ui.ScrollDisabledViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

...other xml layout

答案 1 :(得分:0)

基于public class ScrollDisabledViewPager,我猜测这不是SignUpActivity.java的嵌套类

但是你的错误似乎是指它为一个。

com.lol.firebase.ui.SignUpActivity.ScrollDisabledViewPager

您可以尝试将类移到com.lol.firebase.ui包中,如果没有,但XML必须与package + "." + className匹配