【问题标题】:Error inflating class... (CardView)膨胀类时出错...(CardView)
【发布时间】:2015-11-30 21:06:42
【问题描述】:

当我启动我的应用程序时出现错误

android.view.InflateException: Binary XML file line #2: Error inflating class com.example.tobi.zoom_gallery.SquareCardView

显示出来了。

我想在带有 GridLayout 的 RecyclerView 中显示 3 列 40 x 40 像素的图像。

主活动:

public class MainActivity extends AppCompatActivity {


RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);



    /////////////////////////////////////////////

    recyclerView = (RecyclerView) findViewById(R.id.RV);
    recyclerView.setHasFixedSize(true);

    adapter = new MyAdapter(getResources());
    recyclerView.setAdapter(adapter);

    layoutManager = new GridLayoutManager(this, 3);
    recyclerView.setLayoutManager(layoutManager);

    ///////////////////////////////////////////////////



}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

MyAdapter(用于 RecyclerView):

public class MyAdapter extends RecyclerView.Adapter<MyHolder>{

    ArrayList<String> names;
    Resources resources;

    public MyAdapter(ArrayList<String> names, Resources resources) {
        this.names = names;
        this.resources = resources;
    }

    @Override
    public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.card_view_xml, parent, false);

        return new MyHolder(itemView);

    }

    @Override
    public void onBindViewHolder(MyHolder holder, int position) {

        holder.imageView.setImageBitmap(BitmapFactory.decodeResource(resources, R.drawable.drawable));


    }

    @Override
    public int getItemCount() {
        return 50;
    }
  }

MyHolder(CardView 的 ViewHolder):

public class MyHolder extends RecyclerView.ViewHolder {

    ImageView imageView;

    public MyHolder(View v) {
        super(v);
        imageView = (ImageView) v.findViewById(R.id.imageview);

    }
}

SquareCardView(我使用修改后的 CardView 使其成为正方形;您将在 XML 文件中找到)

public class SquareCardView extends android.support.v7.widget.CardView{

    public SquareCardView(Context context) {
        super(context);
    }

    @Override public void onMeasure(int widthMeasureSpec, int    heightMeasureSpec)        
{
        int width = MeasureSpec.getSize(widthMeasureSpec);
        int height = MeasureSpec.getSize(heightMeasureSpec);
        int size = width > height ? height : width;
        setMeasuredDimension(size, size);
    }
}

XMl 文件(这里我使用 SquareCardView)

<?xml version="1.0" encoding="utf-8"?>
<com.example.user.s.SquareCardView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_margin="5dp"
    android:padding="2dp"
    >

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageview"
        android:adjustViewBounds="true"
        android:scaleType="fitCenter"
        android:layout_gravity="center_horizontal"/>

</com.example.user.s.SquareCardView>

我得到这个错误:

android.view.InflateException: Binary XML file line #2: Error inflating class com.example.tobi.zoom_gallery.SquareCardView
                                                                                   at android.view.LayoutInflater.createView(LayoutInflater.java:616)
                                                                                   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
                                                                                   at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
                                                                                   at com.example.tobi.zoom_gallery.MyAdapter.onCreateViewHolder(MyAdapter.java:30)
                                                                                   at com.example.tobi.zoom_gallery.MyAdapter.onCreateViewHolder(MyAdapter.java:15)

MyAdapter.java:30MyAdapter

中的这段代码
View itemView = LayoutInflater.
                from(parent.getContext()).
                inflate(R.layout.card_view_xml, parent, false);

.

请告诉我为什么 Cardview 不能充气。

【问题讨论】:

    标签: java android xml view android-view


    【解决方案1】:

    要在xml 中使用SquareView,您必须覆盖采用(Context context, AttributeSet attrs) 的构造函数,这是从xml 扩展视图时使用的构造函数。例如

      public SquareCardView(Context context, AttributeSet attrs) {
            super(context, attrs);
      }
    

    【讨论】:

    • 我完全不明白...我现在该怎么办?
    • 将该构造函数添加到您的SquareCardView
    • 谢谢,我添加了那个构造函数...但是现在我不知道为什么,没有显示任何图像...当我启动应用程序时,我得到一个白屏。
    • 如果您不发布您实际使用的代码,很难说出哪里出了问题。例如。你的 MyAdapter 没有构造函数,它只接受一个 Resource 对象。
    • 这是我实际使用的代码。我刚刚更改了 SquareCardView 的构造函数。
    【解决方案2】:

    在 Square 视图的构造函数中,您需要包含参数 AttributeSet attr 才能在 xml 中使用它。

    来自安卓开发者:

        class PieChart extends View {
        public PieChart(Context context, AttributeSet attrs) {
            super(context, attrs);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-29
      • 2017-02-28
      • 2021-07-10
      • 2020-08-01
      • 2016-02-28
      相关资源
      最近更新 更多