这是我的logcat
08-05 09:05:59.833 24812-24812/com.Pi110.KGHS E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
at android.widget.ListView.measureScrapChild(ListView.java:1163)
at android.widget.ListView.measureHeightOfChildren(ListView.java:1246)
at android.widget.ListView.onMeasure(ListView.java:1155)
at android.view.View.measure(View.java:12752)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:594)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:376)
at android.view.View.measure(View.java:12752)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12752)
at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
at android.view.View.measure(View.java:12752)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at android.view.View.measure(View.java:12752)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:812)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
at android.view.View.measure(View.java:12752)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2267)
at android.view.View.measure(View.java:12752)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1074)
at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2462)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4558)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
我甚至没有在我的布局中使用listview,所以我不知道我在哪里得到这个错误。任何帮助都会很棒。它并没有出现在api 22中,但到目前为止在旧的Android版本中都没有。
我添加此代码后似乎已经发生了。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rss_item, parent, false);
holder = new ViewHolder();
if ((position % k) == 0) {
if (convertView instanceof AdView) {
return convertView;
} else {
holder.mAdView = (AdView) convertView.findViewById(R.id.adView);
return holder.mAdView;
}}
else {
convertView = inflater.inflate(R.layout.rss_item, parent, false);
holder.itemTitle = (TextView) convertView.findViewById(R.id.textView);
convertView.setTag(holder);
Typeface face = Typeface.createFromAsset(context.getAssets(), "font/Roboto-Regular.ttf");
holder.itemTitle.setTypeface(face);
holder.itemTitle1 = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
holder.itemTitle2 = (TextView) convertView.findViewById(R.id.textView2);
convertView.setTag(holder);
Typeface face1 = Typeface.createFromAsset(context.getAssets(), "font/Roboto-Thin.ttf");
holder.itemTitle1.setTypeface(face1);
holder.itemTitle2.setVisibility(View.GONE);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
}
EventItem item = itemList.get(position);
holder.itemTitle.setText(item.getTitle());
holder.itemTitle1.setText(item.getDate());
holder.itemTitle2.setText(item.getCategories());
if (item.getIcon()!= 1){
holder.image.setImageResource(item.getIcon());}
return convertView;
}
这是布局
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id= "@+id/layout"
android:background= "@drawable/transition"
android:padding="8dp"
>
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:ellipsize="end"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:layout_below="@+id/icon"
android:textColor="#ff000000"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView"
android:textSize="20sp"
android:textColor="#ff8d8d8d"
android:paddingRight= "10dp"
android:paddingTop="5dp"
android:layout_centerHorizontal="true"
android:paddingBottom="5dp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textView"
android:textColor="#ff8d8d8d"
android:textSize="20sp"
android:paddingRight= "10dp"
android:paddingLeft="10dp"
android:paddingTop="5dp"
android:paddingBottom="5dp"/>
<ImageView
android:id="@+id/icon"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:background="@drawable/customicon"/>
是的,错误就在那里。除了convertView return语句之外我基本上删除了所有行,它很好吗?!!!!! ??????
我的代码似乎很好......我会再次尝试清理它。感谢所有人。
答案 0 :(得分:3)
替换getView()方法的第一行:
convertView = View.inflate(context, R.layout.rss_item, null);
要
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rss_item, parent, false);
问题是列表视图假设getView()方法将返回属于AbsListView
父视图的视图。但是您的代码返回的视图的父级是“RelativeLayout”。