我正在尝试遵循这个示例cardslib 示例,但我遇到了一个异常,我看不出我做错了什么。
这是我的代码:
public class FragmentA extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.cards_teste, container, true);
// CardView cardView = (CardView) inflater.inflate(R.layout.cards_teste, container, false);
ArrayList<Card> cards = new ArrayList<Card>();
//Create a Card
Card card = new Card(getActivity());
//Create a CardHeader
CardHeader header = new CardHeader(getActivity());
//Add Header to card
card.addCardHeader(header);
cards.add(card);
CardArrayAdapter mCardArrayAdapter = new CardArrayAdapter(getActivity(), cards);
CardListView listView = (CardListView) view.findViewById(R.id.myList_teste);
if (listView!=null)
{
listView.setAdapter(mCardArrayAdapter);
}
// return cardView;
return view;
}
}
这是我的 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<it.gmariotti.cardslib.library.view.CardListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/myList_teste"/>
</LinearLayout>
这是堆栈跟踪:
06-05 23:26:54.250: E/AndroidRuntime(3367): FATAL EXCEPTION: main
06-05 23:26:54.250: E/AndroidRuntime(3367): Process: com.mypackage.android.design.appdesgin, PID: 3367
06-05 23:26:54.250: E/AndroidRuntime(3367): android.view.InflateException: Binary XML file line #7: Error inflating class it.gmariotti.cardslib.library.view.CardListView
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.createView(LayoutInflater.java:620)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
06-05 23:26:54.250: E/AndroidRuntime(3367): at com.mypackage.android.design.appdesgin.fragments.FragmentA.onCreateView(FragmentA.java:23)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.Fragment.performCreateView(Fragment.java:1700)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.BackStackRecord.run(BackStackRecord.java:684)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.os.Handler.handleCallback(Handler.java:733)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.os.Handler.dispatchMessage(Handler.java:95)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.os.Looper.loop(Looper.java:136)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.app.ActivityThread.main(ActivityThread.java:5081)
06-05 23:26:54.250: E/AndroidRuntime(3367): at java.lang.reflect.Method.invokeNative(Native Method)
06-05 23:26:54.250: E/AndroidRuntime(3367): at java.lang.reflect.Method.invoke(Method.java:515)
06-05 23:26:54.250: E/AndroidRuntime(3367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
06-05 23:26:54.250: E/AndroidRuntime(3367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
06-05 23:26:54.250: E/AndroidRuntime(3367): at dalvik.system.NativeStart.main(Native Method)
06-05 23:26:54.250: E/AndroidRuntime(3367): Caused by: java.lang.reflect.InvocationTargetException
06-05 23:26:54.250: E/AndroidRuntime(3367): at java.lang.reflect.Constructor.constructNative(Native Method)
06-05 23:26:54.250: E/AndroidRuntime(3367): at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
06-05 23:26:54.250: E/AndroidRuntime(3367): at android.view.LayoutInflater.createView(LayoutInflater.java:594)
06-05 23:26:54.250: E/AndroidRuntime(3367): ... 20 more
06-05 23:26:54.250: E/AndroidRuntime(3367): Caused by: java.lang.NoClassDefFoundError: it.gmariotti.cardslib.library.R$styleable
06-05 23:26:54.250: E/AndroidRuntime(3367): at it.gmariotti.cardslib.library.view.CardListView.initAttrs(CardListView.java:146)
06-05 23:26:54.250: E/AndroidRuntime(3367): at it.gmariotti.cardslib.library.view.CardListView.init(CardListView.java:127)
06-05 23:26:54.250: E/AndroidRuntime(3367): at it.gmariotti.cardslib.library.view.CardListView.<init>(CardListView.java:106)
06-05 23:26:54.250: E/AndroidRuntime(3367): ... 23 more
有人可以解释一下为什么会发生异常以及如何解决它吗?
谢谢
编辑
这是评论中要求的方法。** ** 之间的内容是确切的行
protected void initAttrs(AttributeSet attrs, int defStyle) {
list_card_layout_resourceID = R.layout.list_card_layout;
TypedArray a = getContext().getTheme().obtainStyledAttributes(
**attrs, R.styleable.card_options, defStyle, defStyle);**
try {
list_card_layout_resourceID = a.getResourceId(R.styleable.card_options_list_card_layout_resourceID, this.list_card_layout_resourceID);
} finally {
a.recycle();
}
}