引用xml文件行时,自定义scrollview崩溃,并出现错误放大类

时间:2019-01-03 15:32:14

标签: java android xml android-scrollview

由于我的ScrollView似乎覆盖了布局dynamic_content中动态创建的视图上的点击事件,因此我一直在尝试实现自定义ScrollView。但是,我的应用不断崩溃,并显示错误“ 错误膨胀类”和“ 在路径上找不到类” ...”。

这是xml文件:

<?xml version="1.0" encoding="utf-8"?>

<com.example.test4.MainActivity.CustomScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/dynamic_content_scroll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:context=".MainActivity">

    <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/dynamic_content"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    tools:context=".MainActivity">
    </android.support.constraint.ConstraintLayout>

</com.example.test4.MainActivity.CustomScrollView>

我一直在尝试使用Biraj Zalavadia创建的java代码实现自定义scrollView,该问题是:How to disable and enable the scrolling on android ScrollView?

public class CustomScrollView extends ScrollView {

    private boolean enableScrolling = true;

    public boolean isEnableScrolling() {
        return enableScrolling;
    }

    public void setEnableScrolling(boolean enableScrolling) {
        this.enableScrolling = enableScrolling;
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {

        if (isEnableScrolling()) {
            return super.onInterceptTouchEvent(ev);
        } else {
            return false;
        }
    }
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (isEnableScrolling()) {
            return super.onTouchEvent(ev);
        } else {
            return false;
        }
    }
}

尝试:

我已经尝试过对视图名称com.example.test4.MainActivity.CustomScrollView(这是从Android Studio的自动填充功能接收到的名称)进行各种各样的命名变体,但是我一直收到错误消息“ java.lang.ClassNotFoundException ”。其他变体包括CustomScrollViewcom.example.test4.CustomScrollViewMainActivity.CustomScrollViewtest4.CustomScrollView。另一方面,默认的ScrollView可以工作。

这是logcat:

2019-01-03 22:18:19.496 26790-26790/com.example.test4 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.test4, PID: 26790
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test4/com.example.test4.MainActivity}: android.view.InflateException: Binary XML file line #67: Binary XML file line #3: Error inflating class com.example.test4.MainActivity.CustomScrollView
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2792)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1601)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:172)
    at android.app.ActivityThread.main(ActivityThread.java:6590)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: android.view.InflateException: Binary XML file line #67: Binary XML file line #3: Error inflating class com.example.test4.MainActivity.CustomScrollView
 Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class com.example.test4.MainActivity.CustomScrollView
 Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.test4.MainActivity.CustomScrollView" on path: DexPathList[[zip file "/data/app/com.example.test4-yb5Lg51_OfTw3mpgSsJETw==/base.apk",
 ... (trimmed) ..., nativeLibraryDirectories=[/data/app/com.example.test4-yb5Lg51_OfTw3mpgSsJETw==/lib/arm64, /system/lib64, /system/vendor/lib64]]
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:125)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
    at android.view.LayoutInflater.createView(LayoutInflater.java:606)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)
    at android.view.LayoutInflater.parseInclude(LayoutInflater.java:965)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:859)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.rInflate(LayoutInflater.java:866)
    at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
    at com.example.test4.MainActivity.onCreate(MainActivity.java:327)
    at android.app.Activity.performCreate(Activity.java:7023)
    at android.app.Activity.performCreate(Activity.java:7014)

问题:

基本上,我想修复尝试使用自定义滚动视图时收到的XML派生的错误ClassNotFoundException。

1 个答案:

答案 0 :(得分:1)

通常,将自定义视图子类放入它们自己的.java文件中是一个好主意。看来您的CustomScrollView类可能是MainActivity.java的内部类;尝试将其作为独立类移至CustomScrollView.java

请注意,如果这样做,则必须更新布局<CustomScrollView>标签,因为它们当前引用MainActivity.CustomScrollView