Почему возникает ошибка Error inflating class com.google.android.material.materialswitch.MaterialSwitch? Уже всё пробовал, не помогает
(полный текст ошибки внизу).
Если я добавляю MaterialSwitch
в файл разметки для какой-либо Активности, то всё ок.
Но, если я его добавлю в разметку для item'а RecyclerView, то происходит вышеназванная ошибка при открытии активности.
Все решения, которые я находил, были связаны с наследованием моей темы от темы типа Material.Components
, ещё каких-то Material-тем, от кучи всяких пробовал наследовать, и ничего не работает. В чём ещё может быть причина?
item:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/rootLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:alpha="100"
android:background="#ffffff">
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="24dp"
android:layout_marginTop="12dp"
android:layout_marginBottom="12dp"
android:ellipsize="end"
android:hyphenationFrequency="full"
android:maxWidth="180dp"
android:maxLines="2"
android:text="-"
android:textAlignment="viewStart"
android:textColor="#000000"
android:textSize="@dimen/generalText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints" />
<TextView
android:id="@+id/selectedOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:layout_marginEnd="24dp"
android:layout_marginBottom="12dp"
android:ellipsize="end"
android:hyphenationFrequency="full"
android:maxWidth="130dp"
android:maxLines="2"
android:text="dddfnfhfhddhssiuDFEFEDWUSQWJYQDGFSWQ"
android:textSize="@dimen/generalText"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.materialswitch.MaterialSwitch
android:id="@+id/enableOption"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
</androidx.constraintlayout.widget.ConstraintLayout>
AlarmOptionItem.java:
package thematedev.clock;
public class AlarmOptionItem {
private String name, selectedOption;
private boolean isEnabled;
private int id;
public AlarmOptionItem (int id, String name, String selectedOption) {
this.id = id;
this.name = name;
this.selectedOption = selectedOption;
}
public AlarmOptionItem (int id, String name, boolean isEnabled) {
this.id = id;
this.name = name;
this.isEnabled = isEnabled;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
public String getSelectedOption() {
return selectedOption;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public void setId(int id) {
this.id = id;
}
public int getId() {
return id;
}
public boolean isEnabled() {
return isEnabled;
}
public void setEnabled(boolean enabled) {
isEnabled = enabled;
}
}
И адаптер:
package thematedev.clock;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.google.android.material.materialswitch.MaterialSwitch;
import java.lang.reflect.Type;
import java.util.List;
import thematedev.clock.AlarmOptionItem;
import thematedev.clock.R;
public class AlarmOptionAdapter extends RecyclerView.Adapter<AlarmOptionAdapter.ViewHolder>{
interface OnClickListener{
void onClick(AlarmOptionItem item, int position);
}
private final OnClickListener onClickListener;
private final LayoutInflater inflater;
private static AlarmOptionItem item;
private final List<AlarmOptionItem> items;
AlarmOptionAdapter(Context context, List<AlarmOptionItem> items, OnClickListener onClickListener) {
this.onClickListener = onClickListener;
this.items = items;
this.inflater = LayoutInflater.from(context);
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.alarm_option_recyclerview, parent, false);
return new ViewHolder(view);
}
public void onBindViewHolder(ViewHolder holder, int position) {
item = items.get(holder.getAdapterPosition());
holder.name.setText(item.getName());
//Выбрать, что показать: текствью с выбранной опцией или свич
if (item.getSelectedOption() != null) {
holder.selectedOption.setText(item.getSelectedOption());
} else {
holder.selectedOption.setVisibility(View.GONE);
holder.switcher.setVisibility(View.VISIBLE);
holder.switcher.setChecked(item.isEnabled());
}
holder.itemView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v)
{
onClickListener.onClick(item, holder.getAdapterPosition());
}
});
}
@Override
public int getItemCount() {
return items.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
final TextView name, selectedOption;
final MaterialSwitch switcher;
ViewHolder(View view){
super(view);
switcher = view.findViewById(R.id.enableOption);
name = view.findViewById(R.id.name);
selectedOption = view.findViewById(R.id.selectedOption);
}
}
}
Полный текст ошибки:
android.view.InflateException: Binary XML file line #52: Binary XML file line #52: Error inflating class com.google.android.material.materialswitch.MaterialSwitch
Caused by: android.view.InflateException: Binary XML file line #52: Error inflating class com.google.android.material.materialswitch.MaterialSwitch
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.newInstance0(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
at android.view.LayoutInflater.createView(LayoutInflater.java:658)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:801)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:741)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:874)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:835)
at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at thematedev.clock.AlarmOptionAdapter.onCreateViewHolder(AlarmOptionAdapter.java:44)
at thematedev.clock.AlarmOptionAdapter.onCreateViewHolder(AlarmOptionAdapter.java:23)
at androidx.recyclerview.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:7078)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6235)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6118)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6114)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2303)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1627)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1587)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:665)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4134)
at androidx.recyclerview.widget.RecyclerView.onMeasure(RecyclerView.java:3540)
at android.view.View.measure(View.java:23454)
at androidx.constraintlayout.widget.ConstraintLayout$Measurer.measure(ConstraintLayout.java:811)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measure(BasicMeasure.java:466)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.measureChildren(BasicMeasure.java:134)
at androidx.constraintlayout.core.widgets.analyzer.BasicMeasure.solverMeasure(BasicMeasure.java:278)
at androidx.constraintlayout.core.widgets.ConstraintWidgetContainer.measure(ConstraintWidgetContainer.java:120)
at androidx.constraintlayout.widget.ConstraintLayout.resolveSystem(ConstraintLayout.java:1594)
at androidx.constraintlayout.widget.ConstraintLayout.onMeasure(ConstraintLayout.java:1708)
at android.view.View.measure(View.java:23454)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at androidx.appcompat.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:145)
at android.view.View.measure(View.java:23454)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
at android.view.View.measure(View.java:23454)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:23454)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1565)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
2023-05-01 21:06:41.782 2767-2767 AndroidRuntime thematedev.clock E
at android.widget.LinearLayout.onMeasure(LinearLayout.java:726)
at android.view.View.measure(View.java:23454)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6834)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:847)
at android.view.View.measure(View.java:23454)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2951)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1750)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2038)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1633)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7943)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092)
at android.view.Choreographer.doCallbacks(Choreographer.java:893)
at android.view.Choreographer.doFrame(Choreographer.java:812)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078)
at android.os.Handler.handleCallback(Handler.java:907)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:216)
at android.app.ActivityThread.main(ActivityThread.java:7625)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:247)
at com.google.android.material.internal.ThemeEnforcement.checkAppCompatTheme(ThemeEnforcement.java:212)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:147)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:114)
at com.google.android.material.materialswitch.MaterialSwitch.<init>(MaterialSwitch.java:89)
at com.google.android.material.materialswitch.MaterialSwitch.<init>(MaterialSwitch.java:72)
... 67 more
Источник: Stack Overflow на русском