gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:51:28 28 4
gpt4 key购买 nike

我正在尝试遵循这个例子 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();
}
}

最佳答案

您的主要错误是:java.lang.IllegalStateException:指定的子项已有父项。您必须先对 child 的 parent 调用 removeView()。

这是因为您正试图在同一个 ViewGroup 中对同一个 View 膨胀两次。

如果你想再次膨胀它,你必须创建一个新的实例,或者给一个新的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,我认为它会起作用。

关于android - 错误膨胀类 it.gmariotti.cardslib.library.view.CardListView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24079648/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com