我得到
IllegalArgumentException: cannot have a viewTypeCount <1
为我的列表视图。我知道这是为了list.size()
。但这种情况并不经常发生,只是有时我遇到这个错误并导致应用程序崩溃。我还没有得到它的解,有人知道吗?
更新:这是我得到的
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): FATAL EXCEPTION: main
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.racontrs/com.racontrs.Racontours.City}: java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.os.Looper.loop(Looper.java:130)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at java.lang.reflect.Method.invoke(Method.java:507)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at dalvik.system.NativeStart.main(Native Method)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): Caused by: java.lang.IllegalArgumentException: Can't have a viewTypeCount < 1
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.widget.AbsListView$RecycleBin.setViewTypeCount(AbsListView.java:4387)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.widget.ListView.setAdapter(ListView.java:460)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at com.racontrs.Racontours.City.onCreate(City.java:66)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
10-17 05:34:49.103: ERROR/AndroidRuntime(1683):
... 11 more
对于这一行,我得到
objTourListAdapter = new TourListAdapter(tourList, this);
listView.setAdapter(objTourListAdapter); // getting this when I try to add the adapter
Inside TourListAdapter,
public class TourListAdapter extends BaseAdapter {
protected static final Context TourListAdapter = null;
private ArrayList<Tour> tourList;
private OnClickListener clickListener;
private LayoutInflater mInflater;
ViewHolder holder;
City city;
public TourListAdapter(ArrayList<Tour> tourList, City city) {
this.city = city;
this.tourList = tourList;
mInflater = LayoutInflater.from(city);
clickListener = (OnClickListener) city;
}
@Override
public int getViewTypeCount() {
return tourList.size();
}
@Override
public int getItemViewType(int position) {
return position;
}
@Override
public int getCount() {
return this.tourList.size();
}
@Override
public Object getItem(int position) {
return this.tourList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listbox, null);
convertView.setOnClickListener(clickListener);
holder = new ViewHolder();
holder.view1 = (TextView) convertView.findViewById(R.id.listText);
holder.desc = (TextView) convertView.findViewById(R.id.editText1);
holder.index = (TextView) convertView.findViewById(R.id.index);
holder.desc.setVisibility(View.GONE);
holder.priceBtn = (Button) convertView.findViewById(R.id.prcBtn);
holder.icon = (ImageView) convertView.findViewById(R.id.listIcon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.view1.setText((String) tourList.get(position).getObjtourName());
holder.desc.setText((String) tourList.get(position).getDescription());
holder.priceBtn.setText((String) tourList.get(position).getObjPrice());
holder.priceBtn.setOnClickListener(clickListener);
holder.index.setText(String.valueOf(position));
holder.icon.setImageBitmap(BitmapFactory
.decodeFile((RaconTours.PATH + RaconTours.city + File.separator
+ tourList.get(position).getObjtourName()
+ File.separator + tourList.get(position)
.getThumbnailImageName()).replace(" ", "_")));
return convertView;
}
}
这是我的活动onCreate代码
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.city);
Bundle bundle = getIntent().getExtras();
tourList = (ArrayList<Tour>) bundle.get("arrayTour");
citytextView = (TextView) findViewById(R.id.tourcity);
ImageButton btnBack = (ImageButton) findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setResult(RESULT_OK);
finish();
}
});
btnMap = (ImageButton) findViewById(R.id.btnMap);
btnMap.setOnClickListener(this);
btnUt = (ImageButton) findViewById(R.id.utBtn);
btnUt.setOnClickListener(this);
// setting the City Name
citytextView.setText(RaconTours.city);
/**** Identifying a listView ****/
ListView listView = (ListView) findViewById(R.id.tourList);
/**** Creating a new List Adapter class for binding the values ****/
objTourListAdapter = new TourListAdapter(tourList, this);
listView.setAdapter(objTourListAdapter);
listView.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
metrics = getResources().getDisplayMetrics();
float dpForIcon = 60f;
heightIcon = (int) (metrics.density * dpForIcon + 0.5f);
if (null != RaconTours.dialog) {
RaconTours.dialog.dismiss();
}
}
getViewTypeCount()
应该返回ListView包含的不同视图的数量。如果ListView中的所有项目都是相同的类型,你应该返回1。
这是错误的,
@Override
public int getViewTypeCount() {
return tourList.size();
}
因为当tourList.size()为0时,你会得到这个异常
因为你只有一个列表视图项目的"类型",所以使用这个:
@Override
public int getViewTypeCount() {
return 1;
}
所以我不确定你想做什么,但要理解getViewTypeCount()只在列表视图上调用setAdapter时调用一次。因此,如果此时列表为空或为空,则应用程序将失败。这里返回的值应该被认为是一个常量。
ViewTypes用于视图回收。基本上,通过将其指定为列表的长度,您表示列表中的每个实体都不能重用列表中其他实体使用过的任何其他视图。如果这是真的,那么这种行为是好的,但是最小值必须始终为1。在调用setAdapter之后,这也不能改变,直到你创建一个新的适配器,用新的内容在列表视图上设置(这只是列表适配器中的视图回收器工作)。
我认为你可以通过viewtypeCount为1,因为你所有的对象都是相同的类型。所以不要重写getItemViewType或getItemViewCount()。这些默认值分别为0和1,这将足以满足您发布的适配器代码,并且仍然可以有效地使用视图回收器。
我知道这是一个古老的问题,但是像我一样徘徊在这里的人可以从中受益。你看,我需要使用:
@Override
public int getViewTypeCount() {
return tourList.size();
}
因为这是我的行数据在滚动期间保持其位置的唯一方法。但我有时会遇到崩溃的问题。所以我添加了一些东西来防止崩溃,现在一切都好了:
@Override
public int getViewTypeCount() {
int count;
if (mItems.size() > 0) {
count = getCount();
} else {
count = 1;
}
return count;
}