当我试图使用新的AppCompatActivity
时出现此错误,但仅在较旧的Android API(如16、22或23)中存在。在新的API(如API 26)中,它可以正常工作。 / p>
这是日志:
android.view.InflateException: Binary XML file line #15: Binary XML file line #15: Error inflating class <unknown>
at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
at valientes.valientesappv2.Domain.Mensajes.MensajeriaAdapter.onCreateViewHolder(MensajeriaAdapter.java:37)
at valientes.valientesappv2.Domain.Mensajes.MensajeriaAdapter.onCreateViewHolder(MensajeriaAdapter.java:25)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6519)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5706)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5589)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5585)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2231)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1558)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1518)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:585)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3719)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3436)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3988)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
at android.view.View.layout(View.java:16630)
at android.view.ViewGroup.layout(ViewGroup.java:5437)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
at android.view.Choreographer.doCallbacks(Choreographer.java:670)
at android.view.Choreographer.doFrame(Choreographer.java:606)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class <u
第三行是引发异常的地方:
@Override
public MensajeriaAdapter.MensajesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
return new MensajeriaAdapter.MensajesViewHolder(v);
}
这是布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="@+id/eTEsribirMensaje"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Escribe un mensaje..."
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/bTenviarMensaje"
android:layout_toStartOf="@+id/bTenviarMensaje" />
<Button
android:id="@+id/bTenviarMensaje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Enviar"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appBarLayout"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:navigationIcon="?attr/homeAsUpIndicator"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/ivFoto"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="@mipmap/ic_launcher" />
<TextView
android:id="@+id/tvTitulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center"
android:text="Título"
android:textSize="25dp"
android:textColor="@color/colorWhite"/>
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rvMensajes"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:listitem="@layout/card_view_mensajes"
android:layout_below="@+id/appBarLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="@+id/eTEsribirMensaje">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
</LinearLayout>
这是我在布局的Recyclerview
中使用的cardViews:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5sp"
android:paddingRight="5sp">
<android.support.v7.widget.CardView
android:id="@+id/cvMensaje"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardBackgroundColor="@color/colorWhiteTransparent"
card_view:cardElevation="0dp">
<LinearLayout
android:id="@+id/mensajeBG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/out_message_bg"
android:orientation="vertical">
<TextView
android:id="@+id/msTexto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="240sp"
android:text="hola, qué tal?" />
<TextView
android:id="@+id/msHora"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="12:00 pm"
android:textAppearance="?android:attr/textAppearanceSmall"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
适配器类:
public class MensajeriaAdapter extends RecyclerView.Adapter<MensajeriaAdapter.MensajesViewHolder> {
private List<MensajeDeTexto> mensajeDeTextos;
private Context context;
public MensajeriaAdapter(List<MensajeDeTexto> mensajeDeTextos, Context context) {
this.mensajeDeTextos = mensajeDeTextos;
this.context = context;
}
@Override
public MensajeriaAdapter.MensajesViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
return new MensajeriaAdapter.MensajesViewHolder(v);
}
@Override
public void onBindViewHolder(MensajeriaAdapter.MensajesViewHolder holder, int position) {
RelativeLayout.LayoutParams rl = (RelativeLayout.LayoutParams) holder.cardView.getLayoutParams();
FrameLayout.LayoutParams fl = (FrameLayout.LayoutParams) holder.mensajeBG.getLayoutParams();
LinearLayout.LayoutParams llMensaje = (LinearLayout.LayoutParams) holder.TvMensaje.getLayoutParams();
LinearLayout.LayoutParams llHora = (LinearLayout.LayoutParams) holder.TvHora.getLayoutParams();
if(mensajeDeTextos.get(position).getTipoMensaje()==1){//EMISOR
holder.mensajeBG.setBackgroundResource(R.drawable.in_message_bg);
rl.addRule(RelativeLayout.ALIGN_PARENT_LEFT,0);
rl.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
llMensaje.gravity = Gravity.RIGHT;
llHora.gravity = Gravity.RIGHT;
fl.gravity = Gravity.RIGHT;
holder.TvMensaje.setGravity(Gravity.RIGHT);
}else if(mensajeDeTextos.get(position).getTipoMensaje()==2){//RECEPTOR
holder.mensajeBG.setBackgroundResource(R.drawable.out_message_bg);
rl.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,0);
rl.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
llMensaje.gravity = Gravity.LEFT;
llHora.gravity = Gravity.LEFT;
fl.gravity = Gravity.LEFT;
holder.TvMensaje.setGravity(Gravity.LEFT);
}
holder.cardView.setLayoutParams(rl);
holder.mensajeBG.setLayoutParams(fl);
holder.TvMensaje.setLayoutParams(llMensaje);
holder.TvHora.setLayoutParams(llHora);
holder.TvMensaje.setText(mensajeDeTextos.get(position).getMensaje());
holder.TvHora.setText(mensajeDeTextos.get(position).getHoraDelMensaje());
if(android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) holder.cardView.getBackground().setAlpha(0);
else holder.cardView.setBackgroundColor(ContextCompat.getColor(context,android.R.color.transparent));
}
@Override
public int getItemCount() {
return mensajeDeTextos.size();
}
static class MensajesViewHolder extends RecyclerView.ViewHolder{
CardView cardView;
LinearLayout mensajeBG;
TextView TvMensaje;
TextView TvHora;
MensajesViewHolder(View itemView){
super(itemView);
cardView = (CardView) itemView.findViewById(R.id.cvMensaje);
mensajeBG = (LinearLayout) itemView.findViewById(R.id.mensajeBG);
TvMensaje = (TextView) itemView.findViewById(R.id.msTexto);
TvHora = (TextView) itemView.findViewById(R.id.msHora);
}
}
}
有人知道可能是什么问题吗?
答案 0 :(得分:0)
尝试parent.getApplicationContext()
答案 1 :(得分:0)
您的适配器类中已经有一个上下文。那为什么要尝试从父视图获取上下文呢?
尝试
View v = LayoutInflater.from(context).inflate(R.layout.card_view_mensajes,parent,false);
return new MensajeriaAdapter.MensajesViewHolder(v);
代替
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_mensajes,parent,false);
return new MensajeriaAdapter.MensajesViewHolder(v);
另一个:请尝试在布局背景上使用纯色而不是使用android:background="@drawable/out_message_bg"
。
答案 2 :(得分:0)
我遇到了同样的问题,因为我在项目 xml 中设置了 backgroundTint = ?attr/item_color 但我在主题(夜间)中没有这个颜色参数。添加此颜色修复了错误