自定义滚动视图在引用 xml 文件行时因错误膨胀 class 而崩溃
Custom scrollview crashes with error inflating class while referring to xml file line
因为我的 ScrollView
似乎覆盖了布局 dynamic_content
内动态创建的视图上的点击事件,所以我一直在尝试实现自定义 ScrollView
。但是,我的应用程序不断崩溃并给出错误“膨胀 class”和“未找到 class”... “在路径上”。
这是 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 代码实现自定义滚动视图: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”。其他变体包括 CustomScrollView
、com.example.test4.CustomScrollView
、MainActivity.CustomScrollView
和 test4.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。
一般来说,将自定义视图子 class 放在它们自己的 .java
文件中是个好主意。看起来你的 CustomScrollView
class 可能是 MainActivity.java
的内部 class;尝试将其作为独立 class.
移动到 CustomScrollView.java
请注意,如果您这样做,则必须更新您的布局 <CustomScrollView>
标签,因为它们当前引用 MainActivity.CustomScrollView
.
因为我的 ScrollView
似乎覆盖了布局 dynamic_content
内动态创建的视图上的点击事件,所以我一直在尝试实现自定义 ScrollView
。但是,我的应用程序不断崩溃并给出错误“膨胀 class”和“未找到 class”... “在路径上”。
这是 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 代码实现自定义滚动视图: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”。其他变体包括 CustomScrollView
、com.example.test4.CustomScrollView
、MainActivity.CustomScrollView
和 test4.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。
一般来说,将自定义视图子 class 放在它们自己的 .java
文件中是个好主意。看起来你的 CustomScrollView
class 可能是 MainActivity.java
的内部 class;尝试将其作为独立 class.
CustomScrollView.java
请注意,如果您这样做,则必须更新您的布局 <CustomScrollView>
标签,因为它们当前引用 MainActivity.CustomScrollView
.