2014-11-21 53 views
3

我想在Android的L型的兩個新功能,CardView例如不工作

  • RecyclerView
  • CardView

井RecyclerView得到了me.But工作正常,當我試圖執行CardView.It引發錯誤,我無法找到錯誤是什麼!

MainActivity.java

public class MainActivity extends Activity { 

private RecyclerView mRecyclerView; 
private RecyclerView.Adapter mAdapter; 
private RecyclerView.LayoutManager mLayoutManager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    String[] myDataset ={"Uday","Harihar","chetan","ravi","harish","Rahul","satish","vikaram","ravikiran","harish2"}; 

mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view); 

    // improve performance if you know that changes in content 
    // do not change the size of the RecyclerView 
    mRecyclerView.setHasFixedSize(true); 

    // use a linear layout manager 
    mLayoutManager = new LinearLayoutManager(this); 
    mRecyclerView.setLayoutManager(mLayoutManager); 

    // specify an adapter (see also next example) 
    mAdapter = new MyAdapter(myDataset); 
    mRecyclerView.setAdapter(mAdapter); 

} 

MyAdapter.java

package com.hrh.material; 

import android.support.v7.widget.RecyclerView; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { 
private String[] mDataset; 

// Provide a reference to the views for each data item 
// Complex data items may need more than one view per item, and 
// you provide access to all the views for a data item in a view holder 
public static class ViewHolder extends RecyclerView.ViewHolder { 
    // each data item is just a string in this case 
    public TextView mTextView; 
    public ViewHolder(View v) { 
     super(v); 
     mTextView = (TextView)v.findViewById(R.id.text); 
    } 
} 

// Provide a suitable constructor (depends on the kind of dataset) 
public MyAdapter(String[] myDataset) { 
    mDataset = myDataset; 
} 

// Create new views (invoked by the layout manager) 
@Override 
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, 
               int viewType) { 
    // create a new view 
    View v = LayoutInflater.from(parent.getContext()) 
          .inflate(R.layout.activity_card_view, parent, false); 
    // set the view's size, margins, paddings and layout parameters 

    ViewHolder vh = new ViewHolder(v); 
    return vh; 
} 

// Replace the contents of a view (invoked by the layout manager) 
@Override 
public void onBindViewHolder(ViewHolder holder, int position) { 
    // - get element from your dataset at this position 
    // - replace the contents of the view with that element 
    holder.mTextView.setText(mDataset[position]); 

} 

// Return the size of your dataset (invoked by the layout manager) 
@Override 
public int getItemCount() { 
    return mDataset.length; 
} 


} 

activity_card_view.xml

<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:card_view="http://schemas.android.com/apk/res-auto" 
android:layout_height="match_parent" 
android:layout_width="match_parent" 
> 

     <android.support.v7.widget.CardView 
     android:id="@+id/cardview" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:elevation="100dp" 
     card_view:cardCornerRadius="8dp" 
     > 

     <TextView 
      android:id="@+id/text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" /> 

    </android.support.v7.widget.CardView> 
</RelativeLayout> 

在佈局它表明「資源ID 0x7f040001不是類型STYLE(而不是動物)「。

enter image description here

+0

你是否想爲循環視圖的每一行膨脹scrollview?看起來像一個壞主意,有雙滾動視圖 – 2014-11-21 09:44:43

+0

其實它是RelativeLayout,我現在改變它。 – Uday 2014-11-21 10:19:20

回答

0

我也面臨着同樣的錯誤,我發現這種解決方案只是從SDK中刪除兩個包並安裝again.They被Extras->支持Android庫和Android支持庫

1

我解決問題以下步驟:

  1. 從我的IDE(在我的情況下,Eclipse)中刪除android-support-v7-appcompat和cardview libs項目。
  2. 從Android SDK Manager更新Android SDK工具和Android支持庫。
  3. 重新啓動您的IDE(Eclipse)。
  4. 導入android-support-v7-appcompat和cardview庫項目。在你的XML文件的頂部card_view:

並確保你添加的xmlns

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:card_view="http://schemas.android.com/apk/res-auto" 

<android.support.v7.widget.CardView 
    android:id="@+id/card_view" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    card_view:cardCornerRadius="4dp"> 

和多數民衆贊成它,希望它幫助。

+0

它現在似乎在工作。更新SDK後。 – Uday 2015-02-10 12:54:44