发布了第一个问题,哇!
您好, 我正在实现现在包含在v7 android支持库中的CardView和RecyclerView库,但是当应用程序运行时,它会在充气CardView布局时退出。我已经做了大量的网络搜索并发现了一些有用的东西,但问题仍然存在。
我已根据此处给出的说明导入了CardView和RecyclerView库: https://developer.android.com/tools/support-library/setup.html#libs-with-res(我正在使用Eclipse),然后我将它们添加到我构建的应用程序引用的库列表中(在Project Properties> Android> Libraries下)。 但似乎至少没有正确导入CardView库,或者依赖项是错误的,或者因为代码似乎是正确的。
以下是使用RecyclerView的相关部分片段:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
mLayout = (RelativeLayout) inflater.inflate(R.layout.fragment_jamboree_info, container, false);
return mLayout;
}
@Override
public void onActivityCreated(@Nullable
Bundle savedInstanceState) {
RecyclerView recList = (RecyclerView) mLayout.findViewById(R.id.cardList);
recList.setHasFixedSize(true);
LinearLayoutManager llm = new LinearLayoutManager(getActivity());
llm.setOrientation(LinearLayoutManager.VERTICAL);
recList.setLayoutManager(llm);
// make and set the adapter
CardAdapter adapter = new CardAdapter(mDataSet);
recList.setAdapter(adapter);
super.onActivityCreated(savedInstanceState);
}
这是R.layout.fragment_jamboree_info:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity" >
<android.support.v7.widget.RecyclerView
android:id="@+id/cardList"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity" />
</RelativeLayout>
这是CardAdapter代码:
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
private ArrayList<ParseObject> mDataSet;
public CardAdapter(ArrayList<ParseObject> dataSet) {
mDataSet = dataSet;
}
// Size of the dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataSet.size();
}
/** Provide a reference to the type of views that you are using (custom ViewHolder) */
public static class ViewHolder extends RecyclerView.ViewHolder {
protected TextView vTitle;
public ViewHolder(View v) {
super(v);
vTitle = (TextView) v.findViewById(R.id.TV_title);
}
}
// Create new views (invoked by the layout manager)
@Override
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
LayoutInflater inflater = LayoutInflater.from(viewGroup.getContext());
// ** This is where it crashes ** //
View v = inflater.inflate(R.layout.card_jamboree_info, viewGroup, false);
return new ViewHolder(v);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, final int position) {
viewHolder.vTitle.setText(mDataSet.get(0).getString("title"));
}
}
这里是R.layout.card_jamboree_info - 这个CardView似乎是个问题......
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="8dp"
android:orientation="vertical"
android:background="@color/grey_light"
card_view:cardCornerRadius="5dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/white" >
<TextView
android:id="@+id/TV_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:textSize="32sp"
android:text="@string/basic_error" />
</RelativeLayout>
</android.support.v7.widget.CardView>
最后,冒着太多信息的风险,这里是错误日志:
01-21 23:00:10.964: D/AndroidRuntime(15128): Shutting down VM
01-21 23:00:10.980: E/AndroidRuntime(15128): FATAL EXCEPTION: main
01-21 23:00:10.980: E/AndroidRuntime(15128): Process: com.evoqe, PID: 15128
01-21 23:00:10.980: E/AndroidRuntime(15128): android.view.InflateException: Binary XML file line #2: Error inflating class android.support.v7.widget.CardView
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.LayoutInflater.createView(LayoutInflater.java:633)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.LayoutInflater.inflate(LayoutInflater.java:482)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
01-21 23:00:10.980: E/AndroidRuntime(15128): at com.evoqe.adapters.CardAdapter.onCreateViewHolder(CardAdapter.java:56)
01-21 23:00:10.980: E/AndroidRuntime(15128): at com.evoqe.adapters.CardAdapter.onCreateViewHolder(CardAdapter.java:1)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:4121)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3431)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:3340)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1810)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1306)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1269)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:523)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:1988)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:2237)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:890)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.layoutChildren(FrameLayout.java:573)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.widget.FrameLayout.onLayout(FrameLayout.java:508)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.View.layout(View.java:15596)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewGroup.layout(ViewGroup.java:4966)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2072)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1829)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1054)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5779)
01-21 23:00:10.980: E/AndroidRuntime(15128): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
01-21 23:00:10.980: E/AndroidRuntime(15128): at andro
代码似乎是正确的,所以我怀疑这个问题是依赖性的。我在编写代码时遵循了这两个教程:
http://www.binpress.com/tutorial/android-l-recyclerview-and-cardview-tutorial/156 和 treyrobinson.net/blog/android-l-tutorials-part-3-recyclerview-and-cardview /
任何帮助都非常感谢!提前谢谢。
答案 0 :(得分:0)
我认为您需要在内部导入libs CardView,RecycleView和appcompat_v7,就像首先从项目中删除appcompat_v7 * Project&gt; properties&gt; Android&gt;删除Apply和Ok *然后从你的HardDrive导入reclyce View,Card View和appcompat_v7以及(如果需要的话,appcompat_v4)到你的项目libs文件夹然后从项目&gt;属性&gt; JavaBuildPath&gt;添加Jar而不是Add external然后Jar从项目libs文件夹中选择库。然后,您需要更改styles.xml中的主题。
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
到
<style name="AppBaseTheme" parent="@android:style/Theme.Holo.Light">
和其他styles.xml中的相同
因为删除后不再支持Appcompat.Light。
然后清理并运行。希望它可能有效!
答案 1 :(得分:0)
如果您已经尝试了解决方案中的所有内容,那么需要检查我之前做过的这个小错误。
如果您的清单文件的目标sdk版本为 21 。不低于此。
<uses-sdk
android:minSdkVersion="11" //anything you need
android:targetSdkVersion="21" />
因为在互联网上尝试了所有内容后,我通过将目标从 17 更改为 21 来解决了recyclelerview膨胀错误。
可能这对未来的某些人有帮助,而不是像我一样浪费时间。