1

I'm trying to follow this example cardslib example but I'm running into an exception which I don't see what I'm doing wrong.

Here's my code:

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

And here's my XML file:

<?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>

Here's the stacktrace:

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

Can someone explain me why is the exception occurring and how to fix it?

Thanks

EDIT

Here is the method requested in the comments. What is between ** ** is the exact line

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();
        }
    }
Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
dazito
  • 7,740
  • 15
  • 75
  • 117
  • clean and build your project – Rod_Algonquin Jun 06 '14 at 10:32
  • `at it.gmariotti.cardslib.library.view.CardListView.initAttrs(CardListView.java:146)` can you show us which line is this one? – ahmed_khan_89 Jun 06 '14 at 10:32
  • @Rod_Algonquin it didn't help, I cleaned and built my project to no success – dazito Jun 06 '14 at 10:39
  • perfect try this (change the `defStyle` by 0) , like this: `TypedArray a = getContext().getTheme().obtainStyledAttributes( attrs, R.styleable.card_options, 0, 0);` . – ahmed_khan_89 Jun 06 '14 at 10:42
  • @ahmed_khan_89 now I get this exception: `java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first. ` but I don't think I should be editing the library as it may fix this error but propagate a lot more errors in the future. It should work as it is in my opinion, therefore I believe the error is somewhere on my code and not the library's code – dazito Jun 06 '14 at 10:46
  • @ahmed_khan_89 it works as you suggested, nevermind that exception. That was me trying something else (I passed true to `attachToRoot` on the inflate method, with `false` it works but I don't feel comfortable changing the library code – dazito Jun 06 '14 at 10:54
  • I wanted to test it, to know the origin of the real error. so your principal error is : `java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.` I will write you an answer. – ahmed_khan_89 Jun 06 '14 at 11:01
  • 1
    @ahmed_khan_89 please check the comment above yours. That 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. But for that I've to set `defStyle` to zero in the library code, as your suggestion – dazito Jun 06 '14 at 11:04
  • Is there any Android card UI library which provides method for swapping between cards? – Nevaeh Aug 19 '14 at 10:59

1 Answers1

1

Your principal error is : java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

It is because you are trying to inflate the same View twice in the same ViewGroup.

If you want to inflate it again , you have to create a new instance, or give a new ID , or remove the old one...

OK , now when I am writing this , you fixed this bug saying in comments :

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.

If you try to put again the defStyle, I think it would work.

ahmed_khan_89
  • 2,755
  • 26
  • 49