错误膨胀类it.gmariotti.cardslib.library.view.CardListView

时间:2014-06-06 10:29:16

标签: android android-layout android-cards

我正试图遵循这个例子cardslib example,但我遇到了一个例外情况,我看不出我做错了什么。

这是我的代码:

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();
        }
    }

1 个答案:

答案 0 :(得分:1)

您的主要错误是:java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

这是因为您试图在同一View内两次充实相同的ViewGroup

如果您想再次充气,您必须创建一个新实例,或者提供一个新ID,或删除旧ID ...

好的,现在当我写这篇文章时,你在评论中修正了这个错误:

hat exception was me that forgot to pass attachToRoot as false in the inflate method. With attachToRoot as true no exception is generated and the layout is inflated successfully.

如果你再试一次defStyle,我认为它会起作用。