Search code examples
javaandroidxmlandroid-scrollview

Custom scrollview crashes with error inflating class while referring to xml file line


since my ScrollView seems to override click events on the dynamically created views inside the layout dynamic_content inside it, I have been trying to implement a custom ScrollView. However, my app keeps crashing and giving the error "Error inflating class", and "Didn't find class "..." on path".

Here is the xml file:

<?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>

I have been trying to implement a Custom scrollView with java code created by Biraj Zalavadia in this question: 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;
        }
    }
}

Attempts:

I have tried a myriad of naming variations in the view name com.example.test4.MainActivity.CustomScrollView (this is the name received from Android Studio's autofill function), but I keep receiving the error "java.lang.ClassNotFoundException". Other variants include CustomScrollView, com.example.test4.CustomScrollView, MainActivity.CustomScrollView, and test4.CustomScrollView. On the other hand, the default ScrollView works.

Here is the 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)

Problem:

Basically, I want to fix the xml-derived error ClassNotFoundException received when trying to use a custom scrollview.


Solution

  • In general, it is a good idea to put custom view subclasses in their own .java files. It looks like your CustomScrollView class might be an inner class of MainActivity.java; try moving it to CustomScrollView.java as a standalone class.

    Note that if you do this, you'll have to update your layout <CustomScrollView> tags, since they currently reference MainActivity.CustomScrollView.