2015-11-30 45 views
1

當我開始我的應用程序錯誤錯誤充氣類...(CardView)

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

所示。

我想用GridLayout在RecyclerView中顯示3列40 x 40像素的圖像。

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(對於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(該ViewHolder用於CardView):

public class MyHolder extends RecyclerView.ViewHolder { 

    ImageView imageView; 

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

    } 
} 

SquareCardView(ⅰ使用IA改性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:30是這段代碼MyAdapter

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

請告訴我爲什麼Cardview不能被燙傷。

回答

2

使用在SquareViewxml你必須重寫需要(Context context, AttributeSet attrs)也就是當畫面從XML充氣使用的一個構造函數。例如。

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

我不明白到底......我應該什麼了吧? –

+0

在您的'SquareCardView'中添加構造函數 – Blackbelt

+0

謝謝,我加入了構造函數......但現在我不知道爲什麼,沒有顯示任何圖像......當我開始應用程序時,我得到一個白色的屏幕。 –

0

在您的Square視圖的構造函數中,您需要包含參數AttributeSet attr以便能夠在xml中使用它。

從Android開發人員:

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