问题
i am using a BaseExpandableListAdapter as follows. where instead of passing a list, i am directly passing a View to be a child data. and the child count is always returned as 1. as there is only one view per heading.
public class TaxAdapter extends BaseExpandableListAdapter{
private Context _context;
private ArrayList<String> _listDataHeader;
private HashMap<String, View> _listDataChild;
public TaxAdapter(Context context, ArrayList<String> taxListDataHeader,
HashMap<String, View> taxListDataChild) {
this._context = context;
this._listDataHeader = taxListDataHeader;
this._listDataChild = taxListDataChild;
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public int getChildrenCount(int groupPosition) {
return 1;
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition));
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LinearLayout earningsContainer = new LinearLayout(_context);
LayoutInflater inflator = ((Activity)_context).getLayoutInflater();
convertView = inflator.inflate(R.layout.row_tax_explv_title,
earningsContainer, false);
((TextView) convertView.findViewById(R.id.explvTaxTitleText)).setText(headerTitle);
}
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
return (View)getChild(groupPosition, childPosition);
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return false;
}
}
and the data being passed from the activity is as follows -
ArrayList<String> TaxListDataHeader = new ArrayList<String>();
HashMap<String, View> TaxListDataChild = new HashMap<String, View>();
TaxListDataHeader.add("Movie Shows");
TaxListDataChild.put("Movie Shows", moviesContainerView);
TaxListDataHeader.add("Theatre Shows");
TaxListDataHeader.add("Theatre Shows", theatreContainerView);
movieContainerView and theareContainerView are the two custom inflated linear layouts.like this
LayoutInflater inflator = ((Activity) context).getLayoutInflater();
View row = inflator.inflate(R.layout.row_movie_data,
movieMainContainer, false);
((TextView) row.findViewById(R.id.txtEmpInfoPopUpTitle)).setText(key);
((TextView) row.findViewById(R.id.txtEmpInfoPopUpValue)).setText(value);
return row;
i am getting the error as in the title, but the logs do not specify any file name as such. Please suggest where the error might be occuring. Please suggest what can be done.
EDIT: This is the stack trace
07-17 22:53:57.043: E/AndroidRuntime(5074): FATAL EXCEPTION: main
07-17 22:53:57.043: E/AndroidRuntime(5074): Process: com.synthesize.paysal, PID: 5074
07-17 22:53:57.043: E/AndroidRuntime(5074): java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ListView.measureScrapChild(ListView.java:1183)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ListView.onMeasure(ListView.java:1149)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ScrollView.measureChildWithMargins(ScrollView.java:1226)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.ScrollView.onMeasure(ScrollView.java:326)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:719)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:455)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.View.measure(View.java:16497)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer.doFrame(Choreographer.java:544)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Handler.handleCallback(Handler.java:733)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Handler.dispatchMessage(Handler.java:95)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.os.Looper.loop(Looper.java:136)
07-17 22:53:57.043: E/AndroidRuntime(5074): at android.app.ActivityThread.main(ActivityThread.java:5017)
07-17 22:53:57.043: E/AndroidRuntime(5074): at java.lang.reflect.Method.invokeNative(Native Method)
07-17 22:53:57.043: E/AndroidRuntime(5074): at java.lang.reflect.Method.invoke(Method.java:515)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
07-17 22:53:57.043: E/AndroidRuntime(5074): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
07-17 22:53:57.043: E/AndroidRuntime(5074): at dalvik.system.NativeStart.main(Native Method)
EDIT: i have tried to wrap the linear layout with a list view like this,
TaxListDataChild.put("Theatre Show", (new ListView(context)).addView(theatrerowContainer, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
but it seems that the second parameter is void.
I am populating the "new Linearlayout" (earningsContainer) in the xml like this with other linear layout rows.
for (int i = 0; i < taxMainObject.getTaxEarnings().size(); i++) {
earningsContainer.addView(getTaxEarningsRow(taxMainObject
.getTaxEarnings().get(i)));
totalEarnings += taxMainObject.getTaxEarnings().get(i)
.getTotalAmount();
}
earningsContainer.addView(getTaxTotalRow("GROSS TOTAL SALARY",
totalEarnings + ""));
and the xml for the rows is -
<?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="wrap_content"
android:gravity="right"
android:orientation="horizontal"
android:background="#ffeeeeee" >
<TextView
android:id="@+id/txtTotalText"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:textAppearance="@android:attr/textAppearanceSmall"
android:textColor="#736F6E"
android:textSize="14sp"
android:textStyle="bold|italic" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right|center_vertical"
android:orientation="horizontal"
android:padding="2dp" >
<TextView
android:id="@+id/txtTotalValue"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textAppearance="@android:attr/textAppearanceSmall"
android:textColor="#736F6E"
android:gravity="right"
android:textSize="14sp"
android:layout_weight="1"
android:textStyle="italic" />
</LinearLayout>
</LinearLayout>
回答1:
Although this is not related to your java but i still got same error. I had "extends AppCompatActivity" in my class but when i replaced it with "Extends Activity" Error was resolved. Hope this is helpful for someone else, as i was stuck for three hours on this. Happy to help
回答2:
android.widget.ListView$LayoutParams is subclass of android.widget.ViewGroup$LayoutParams while android.widget.AbsListView is also subclass of same one i.e sibling relationship hence type casting will throw an exception. Reference
http://developer.android.com/reference/android/widget/AbsListView.LayoutParams.html
This suggests you can't pass a view directly as a child to listview adapter instead try passing Hashmap and inflate the view dynamically. Try this :
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
Where listDataHeader is a hashmap passed to adapter like
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
(Passed HashMap Instead of View)
来源:https://stackoverflow.com/questions/24831326/java-lang-classcastexception-android-widget-linearlayoutlayoutparams-cannot-be