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