android.widget.listview 无法转换为 android.widget.expandablelistview
android.widget.listview cannot be cast to android.widget.expandablelistview
在我的 android 应用程序中,我将适配器从 ListViewAdapter 更改为 ExpendableListViewAdapter。在此之前,ListViewAdapter 仅一个接一个地加载产品。但是现在我想把这些产品分成几组。
这就是我在 ProductActivity.java 中的 ExpendableListViewAdapter:
class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private Map<String, List<Product>> _listDataChild;
private boolean viewOnly;
private Config config;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
Map<String, List<Product>> listChildData, boolean viewOnly, Config config) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this.viewOnly = viewOnly;
this.config = config;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
Product product = (Product) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.product_item, null);
CheckBox name = (CheckBox) convertView.findViewById(R.id.product_item_checkbox);
TextView price_value = (TextView) convertView.findViewById(R.id.product_item_price_value);
TextView price_value_vo = (TextView) convertView.findViewById(R.id.product_item_price_value_vo);
TextView price_textview = (TextView) convertView.findViewById(R.id.product_item_price_textview);
DatePicker from = (DatePicker) convertView.findViewById(R.id.product_item_datePicker_from);
EditText serial_number = (EditText) convertView.findViewById(R.id.product_item_edittext);
serial_number.addTextChangedListener(new GenericTextWatcherCustom(product));
TextView item_lenght = (TextView) convertView.findViewById(R.id.product_item_lenght);
TextView item_lenght_vo = (TextView) convertView.findViewById(R.id.product_item_lenght_vo);
TextView item_type = (TextView) convertView.findViewById(R.id.product_item_type);
TextView item_type_vo = (TextView) convertView.findViewById(R.id.product_item_type_vo);
TextView name_only = (TextView) convertView.findViewById(R.id.product_item_name);
name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Product product = (Product) cb.getTag();
product.setSelected(cb.isChecked());
}
});
DatePicker.OnDateChangedListener mDateListenerFrom = new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Product product = (Product) view.getTag();
@SuppressWarnings("deprecation")
Date date = new Date(year - 1900,monthOfYear,dayOfMonth);
product.setFrom(date);
}
};
Calendar cal = product.getActive_toDate();
if (cal == null){
cal = Calendar.getInstance();
}
Date date = new Date(cal.get(Calendar.YEAR) - 1900,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
from.setMinDate(date.getTime());
from.init(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), mDateListenerFrom);
product.setFrom(date);
//quantity.setTag(product);
name.setTag(product);
from.setTag(product);
name.setChecked(product.getSelected());
name.setText(product.getName());
name_only.setText(product.getName());
name_only.setVisibility(8);
item_lenght_vo.setVisibility(8);
item_type_vo.setVisibility(8);
price_value.setText(product.getPrice());
price_value_vo.setText(product.getPrice());
price_value_vo.setVisibility(8);
if (product.getUnit().compareTo("hardware") == 0){
from.setVisibility(DatePicker.INVISIBLE);
serial_number.setVisibility(EditText.VISIBLE);
serial_number.setText(product.getSerial_number());
item_lenght_vo.setText(product.getUnit());
item_lenght.setText(product.getUnit());
item_type.setText("Serial number");
}else{
serial_number.setVisibility(DatePicker.INVISIBLE);
from.setVisibility(EditText.VISIBLE);
if (config.getConfig_change_sales_date().compareTo("0") == 0){
from.setEnabled(false);
}
item_lenght.setText(product.getUnit_length() + " " + product.getUnit());
item_lenght_vo.setText(product.getUnit_length() + " " + product.getUnit());
item_type.setText("Product start");
}
if (viewOnly){
name_only.setVisibility(0);
name.setVisibility(8);
from.setVisibility(8);
serial_number.setVisibility(8);
item_type_vo.setVisibility(0);
item_type.setVisibility(8);
item_lenght.setVisibility(8);
item_lenght_vo.setVisibility(0);
item_lenght_vo.setTextColor(Color.LTGRAY);
price_value.setVisibility(8);
price_value_vo.setVisibility(0);
price_value_vo.setTextColor(Color.LTGRAY);
price_textview.setVisibility(8);
historyButton.setVisibility(8);
// Height adjustment of product box
convertView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
// backButton to the whole width
backButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
// Set the same width
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
name_only.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
item_lenght_vo.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
if(name_only.getMeasuredWidth() < item_lenght_vo.getMeasuredWidth()) {
name_only.setLayoutParams(new RelativeLayout.LayoutParams(item_lenght_vo.getMeasuredWidth(), LayoutParams.WRAP_CONTENT));
}
else {
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(name_only.getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.BELOW, R.id.product_item_name);
rl.topMargin = 10;
item_lenght_vo.setLayoutParams(rl);
};
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(70, 0, 0, 0);
params.addRule(RelativeLayout.RIGHT_OF, R.id.product_item_name);
item_type_vo.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.setMargins(70, 10, 0, 0);
params2.addRule(RelativeLayout.BELOW, R.id.product_item_name);
params2.addRule(RelativeLayout.RIGHT_OF, R.id.product_item_name);
price_value_vo.setLayoutParams(params2);
};
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
Product product = (Product) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_separator, null);
}
Button group_name = (Button) convertView.findViewById(R.id.product_group_name);
group_name.setText(product.getGroup_name());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
但是当我启动 ProductActivity.java 它抛出这个异常:
01-30 11:27:36.736: E/AndroidRuntime(1059): FATAL EXCEPTION: main
01-30 11:27:36.736: E/AndroidRuntime(1059): Process: com.example.printhelper, PID: 1059
01-30 11:27:36.736: E/AndroidRuntime(1059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.printhelper/com.example.printhelper.ProductActivity}: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.ExpandableListView
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.access0(ActivityThread.java:135)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.os.Looper.loop(Looper.java:136)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 11:27:36.736: E/AndroidRuntime(1059): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:27:36.736: E/AndroidRuntime(1059): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 11:27:36.736: E/AndroidRuntime(1059): at dalvik.system.NativeStart.main(Native Method)
01-30 11:27:36.736: E/AndroidRuntime(1059): Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.ExpandableListView
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.example.printhelper.ProductActivity.onCreate(ProductActivity.java:131)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.Activity.performCreate(Activity.java:5231)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-30 11:27:36.736: E/AndroidRuntime(1059): ... 11 more
ProductActivity.java 中的第 131 行是这样的:
productList = (ExpandableListView)findViewById(R.id.detail_listView);
这是所有适配器变量的声明:
public class ProductActivity extends Activity {
private ExpandableListView productList;
ExpandableListAdapter dataAdapter = null;
Map<String, List<Product>> product = new HashMap<String, List<Product>>();
List<String> groups;
protected void onCreate(Bundle savedInstanceState) {
...
...
productList = (ExpandableListView)findViewById(R.id.detail_listView);
productList.setItemsCanFocus(true);
createProductList();
}
public void createProductList(){
...
dataAdapter = new ExpandableListAdapter(this,groups,product,viewOnly,config);
productList.setAdapter(dataAdapter);
}
class ExpandableListAdapter extends BaseExpandableListAdapter {
...
...
}
}
真不知道怎么回事。而且也不知道适配器写的好不好...
编辑:product.xml 布局:
....
<ExpandableListView
android:id="@+id/detail_listView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="46"
android:background="@color/dark_gray"
android:choiceMode="none"
android:drawSelectorOnTop="false"
android:descendantFocusability="beforeDescendants"
/>
...
如日志:
ListView cannot be cast to android.widget.ExpandableListView
意味着 detail_listView
是 ListView
而不是 Activity 的 xml 布局中的 ExpandableListView
。
在xml中使用ExpandableListView:
<ExpandableListView android:id="@+id/detail_listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
无法将 ListView 转换为 ExpandableListView。您必须使用 ExpandableListView 来设置此适配器。
在我的 android 应用程序中,我将适配器从 ListViewAdapter 更改为 ExpendableListViewAdapter。在此之前,ListViewAdapter 仅一个接一个地加载产品。但是现在我想把这些产品分成几组。
这就是我在 ProductActivity.java 中的 ExpendableListViewAdapter:
class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private Map<String, List<Product>> _listDataChild;
private boolean viewOnly;
private Config config;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
Map<String, List<Product>> listChildData, boolean viewOnly, Config config) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
this.viewOnly = viewOnly;
this.config = config;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
Product product = (Product) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.product_item, null);
CheckBox name = (CheckBox) convertView.findViewById(R.id.product_item_checkbox);
TextView price_value = (TextView) convertView.findViewById(R.id.product_item_price_value);
TextView price_value_vo = (TextView) convertView.findViewById(R.id.product_item_price_value_vo);
TextView price_textview = (TextView) convertView.findViewById(R.id.product_item_price_textview);
DatePicker from = (DatePicker) convertView.findViewById(R.id.product_item_datePicker_from);
EditText serial_number = (EditText) convertView.findViewById(R.id.product_item_edittext);
serial_number.addTextChangedListener(new GenericTextWatcherCustom(product));
TextView item_lenght = (TextView) convertView.findViewById(R.id.product_item_lenght);
TextView item_lenght_vo = (TextView) convertView.findViewById(R.id.product_item_lenght_vo);
TextView item_type = (TextView) convertView.findViewById(R.id.product_item_type);
TextView item_type_vo = (TextView) convertView.findViewById(R.id.product_item_type_vo);
TextView name_only = (TextView) convertView.findViewById(R.id.product_item_name);
name.setOnClickListener( new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v ;
Product product = (Product) cb.getTag();
product.setSelected(cb.isChecked());
}
});
DatePicker.OnDateChangedListener mDateListenerFrom = new DatePicker.OnDateChangedListener() {
@Override
public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Product product = (Product) view.getTag();
@SuppressWarnings("deprecation")
Date date = new Date(year - 1900,monthOfYear,dayOfMonth);
product.setFrom(date);
}
};
Calendar cal = product.getActive_toDate();
if (cal == null){
cal = Calendar.getInstance();
}
Date date = new Date(cal.get(Calendar.YEAR) - 1900,cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));
from.setMinDate(date.getTime());
from.init(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), mDateListenerFrom);
product.setFrom(date);
//quantity.setTag(product);
name.setTag(product);
from.setTag(product);
name.setChecked(product.getSelected());
name.setText(product.getName());
name_only.setText(product.getName());
name_only.setVisibility(8);
item_lenght_vo.setVisibility(8);
item_type_vo.setVisibility(8);
price_value.setText(product.getPrice());
price_value_vo.setText(product.getPrice());
price_value_vo.setVisibility(8);
if (product.getUnit().compareTo("hardware") == 0){
from.setVisibility(DatePicker.INVISIBLE);
serial_number.setVisibility(EditText.VISIBLE);
serial_number.setText(product.getSerial_number());
item_lenght_vo.setText(product.getUnit());
item_lenght.setText(product.getUnit());
item_type.setText("Serial number");
}else{
serial_number.setVisibility(DatePicker.INVISIBLE);
from.setVisibility(EditText.VISIBLE);
if (config.getConfig_change_sales_date().compareTo("0") == 0){
from.setEnabled(false);
}
item_lenght.setText(product.getUnit_length() + " " + product.getUnit());
item_lenght_vo.setText(product.getUnit_length() + " " + product.getUnit());
item_type.setText("Product start");
}
if (viewOnly){
name_only.setVisibility(0);
name.setVisibility(8);
from.setVisibility(8);
serial_number.setVisibility(8);
item_type_vo.setVisibility(0);
item_type.setVisibility(8);
item_lenght.setVisibility(8);
item_lenght_vo.setVisibility(0);
item_lenght_vo.setTextColor(Color.LTGRAY);
price_value.setVisibility(8);
price_value_vo.setVisibility(0);
price_value_vo.setTextColor(Color.LTGRAY);
price_textview.setVisibility(8);
historyButton.setVisibility(8);
// Height adjustment of product box
convertView.setLayoutParams(new ListView.LayoutParams(ListView.LayoutParams.MATCH_PARENT, ListView.LayoutParams.WRAP_CONTENT));
// backButton to the whole width
backButton.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
// Set the same width
Display display = getWindowManager().getDefaultDisplay();
@SuppressWarnings("deprecation")
int width = display.getWidth();
name_only.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
item_lenght_vo.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
if(name_only.getMeasuredWidth() < item_lenght_vo.getMeasuredWidth()) {
name_only.setLayoutParams(new RelativeLayout.LayoutParams(item_lenght_vo.getMeasuredWidth(), LayoutParams.WRAP_CONTENT));
}
else {
RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(name_only.getMeasuredWidth(), LayoutParams.WRAP_CONTENT);
rl.addRule(RelativeLayout.BELOW, R.id.product_item_name);
rl.topMargin = 10;
item_lenght_vo.setLayoutParams(rl);
};
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.setMargins(70, 0, 0, 0);
params.addRule(RelativeLayout.RIGHT_OF, R.id.product_item_name);
item_type_vo.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.setMargins(70, 10, 0, 0);
params2.addRule(RelativeLayout.BELOW, R.id.product_item_name);
params2.addRule(RelativeLayout.RIGHT_OF, R.id.product_item_name);
price_value_vo.setLayoutParams(params2);
};
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
Product product = (Product) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.group_separator, null);
}
Button group_name = (Button) convertView.findViewById(R.id.product_group_name);
group_name.setText(product.getGroup_name());
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
但是当我启动 ProductActivity.java 它抛出这个异常:
01-30 11:27:36.736: E/AndroidRuntime(1059): FATAL EXCEPTION: main
01-30 11:27:36.736: E/AndroidRuntime(1059): Process: com.example.printhelper, PID: 1059
01-30 11:27:36.736: E/AndroidRuntime(1059): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.printhelper/com.example.printhelper.ProductActivity}: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.ExpandableListView
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.access0(ActivityThread.java:135)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.os.Handler.dispatchMessage(Handler.java:102)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.os.Looper.loop(Looper.java:136)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.main(ActivityThread.java:5017)
01-30 11:27:36.736: E/AndroidRuntime(1059): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 11:27:36.736: E/AndroidRuntime(1059): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
01-30 11:27:36.736: E/AndroidRuntime(1059): at dalvik.system.NativeStart.main(Native Method)
01-30 11:27:36.736: E/AndroidRuntime(1059): Caused by: java.lang.ClassCastException: android.widget.ListView cannot be cast to android.widget.ExpandableListView
01-30 11:27:36.736: E/AndroidRuntime(1059): at com.example.printhelper.ProductActivity.onCreate(ProductActivity.java:131)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.Activity.performCreate(Activity.java:5231)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-30 11:27:36.736: E/AndroidRuntime(1059): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
01-30 11:27:36.736: E/AndroidRuntime(1059): ... 11 more
ProductActivity.java 中的第 131 行是这样的:
productList = (ExpandableListView)findViewById(R.id.detail_listView);
这是所有适配器变量的声明:
public class ProductActivity extends Activity {
private ExpandableListView productList;
ExpandableListAdapter dataAdapter = null;
Map<String, List<Product>> product = new HashMap<String, List<Product>>();
List<String> groups;
protected void onCreate(Bundle savedInstanceState) {
...
...
productList = (ExpandableListView)findViewById(R.id.detail_listView);
productList.setItemsCanFocus(true);
createProductList();
}
public void createProductList(){
...
dataAdapter = new ExpandableListAdapter(this,groups,product,viewOnly,config);
productList.setAdapter(dataAdapter);
}
class ExpandableListAdapter extends BaseExpandableListAdapter {
...
...
}
}
真不知道怎么回事。而且也不知道适配器写的好不好...
编辑:product.xml 布局:
....
<ExpandableListView
android:id="@+id/detail_listView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="46"
android:background="@color/dark_gray"
android:choiceMode="none"
android:drawSelectorOnTop="false"
android:descendantFocusability="beforeDescendants"
/>
...
如日志:
ListView cannot be cast to android.widget.ExpandableListView
意味着 detail_listView
是 ListView
而不是 Activity 的 xml 布局中的 ExpandableListView
。
在xml中使用ExpandableListView:
<ExpandableListView android:id="@+id/detail_listView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
无法将 ListView 转换为 ExpandableListView。您必须使用 ExpandableListView 来设置此适配器。