当我尝试在Android Studio - Design中预览我的 settings.xml 时,它显示错误:**未找到com.mypackage.preferences.SwitchPreferenceCompatFix。**但它存在。
更详细的错误:
java.lang.ClassNotFoundException: com.mypackage.preferences.SwitchPreferenceCompatFix not found.
at com.android.tools.idea.rendering.LayoutlibCallbackImpl.findClass(LayoutlibCallbackImpl.java:836)
at com.android.layoutlib.bridge.android.BridgeContext$1.findClass(BridgeContext.java:642)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:587)
at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:520)
at java.base/java.lang.Class.forName0(Native Method)
at java.base/java.lang.Class.forName(Class.java:467)
at androidx.preference.PreferenceInflater.createItem(PreferenceInflater.java:218)
at androidx.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:281)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:349)
at androidx.preference.PreferenceInflater.rInflate(PreferenceInflater.java:351)
at androidx.preference.PreferenceInflater.inflate(PreferenceInflater.java:161)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:568)
at com.android.layoutlib.common.util.ReflectionUtils.invoke(ReflectionUtils.java:80)
at com.android.layoutlib.bridge.android.support.SupportPreferencesUtil.createInflatedPreference(SupportPreferencesUtil.java:106)
at com.android.layoutlib.bridge.android.support.SupportPreferencesUtil.inflatePreference(SupportPreferencesUtil.java:285)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:351)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:450)
at com.android.tools.idea.layoutlib.LayoutLibrary.createSession(LayoutLibrary.java:122)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:701)
at com.android.tools.idea.rendering.RenderTask.lambda$inflate$8(RenderTask.java:848)
at com.android.tools.idea.rendering.RenderExecutor$runAsyncActionWithTimeout$3.run(RenderExecutor.kt:200)
at com.android.tools.idea.rendering.RenderExecutor$PriorityRunnable.run(RenderExecutor.kt:298)
at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
at java.base/java.lang.Thread.run(Thread.java:833)
- settings.xml*:
<?xml version="1.0" encoding="utf-8"?>
<androidx.preference.PreferenceScreen
xmlns:android="http://schemas.android.com/apk/res/android"
android:key="root">
<PreferenceCategory
android:key="cat_main"
android:title="@string/pref_settings">
<com.mypackage.preferences.SwitchPreferenceCompatFix
android:icon="@drawable/ic_pref"
android:key="prefKey"
android:title="@string/pref_title"
android:defaultValue="true"
android:summary="@string/pref_sum"/>
</PreferenceCategory>
</androidx.preference.PreferenceScreen>
- SwitchPreferenceCompatFix.java*:
package com.mypackage.preferences;
import android.content.Context;
import androidx.preference.PreferenceViewHolder;
import androidx.preference.SwitchPreferenceCompat;
import android.util.AttributeSet;
import com.mypackage.R;
public class SwitchPreferenceCompatFix extends SwitchPreferenceCompat {
private final Context context;
public SwitchPreferenceCompatFix(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
this.context = context;
}
public SwitchPreferenceCompatFix(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
}
public SwitchPreferenceCompatFix(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
}
public SwitchPreferenceCompatFix(Context context) {
super(context);
this.context = context;
}
@Override
public void onBindViewHolder(PreferenceViewHolder holder) {
holder.itemView.setPadding((int) context.getResources().getDimension(R.dimen.preference_side_padding), 0,
(int) context.getResources().getDimension(R.dimen.preference_side_padding), 0);
super.onBindViewHolder(holder);
}
}
Android Studio版本:
Android Studio Giraffe| 2022.3.1 Beta 2 Build #AI-223.8836.35.2231.10023527,构建于2023年4月27日运行时版本:17.0.6+0-b2043.56-9884540 amd64 VM:OpenJDK 64-Bit Server VM by JetBrains s.r.o.
我做错了什么?看起来像是Android Studio的问题,但我不确定
1条答案
按热度按时间bxjv4tth1#
好吧,花了一点时间,找到了解决方案。这是因为旧版本的Gradle。谁能想到。升级到7.4.2版本解决了这个问题。