When i start my app the error
android.view.InflateException: Binary XML file line #2: Error inflating class com.example.tobi.zoom_gallery.SquareCardView
is shown. I want to show 3 collumns of 40 x 40px images in a RecyclerView with GridLayout.
MainActivity:
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 (for 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 (the ViewHolder for a CardView):
public class MyHolder extends RecyclerView.ViewHolder { ImageView imageView; public MyHolder(View v) { super(v); imageView = (ImageView) v.findViewById(R.id.imageview); } }
SquareCardView (i use i a modified CardView to make it square; you'll se in the XML-file)
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); } }
The XMl-file (here i use the 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>
And i get this error:
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:30 is this piece of code in MyAdapter
View itemView = LayoutInflater. from(parent.getContext()). inflate(R.layout.card_view_xml, parent, false);
.
Please tell me why the Cardview can't be inflaten.
No comments:
Post a Comment