获取 viewTypeCount <有时列表视图出现 1 个错误
我正在得到
IllegalArgumentException:不能有 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();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
getViewTypeCount()
应该返回 ListView 包含的不同视图的数量。如果 ListView 中的所有项目都是同一类型,则应该返回 1。
这是错误的,
因为当tourList.size()为零时,您将得到此异常
因为您只有一种“类型”的列表视图项目,请使用此:
getViewTypeCount()
should return the number of different views your ListView contains.If all of items in your ListView are the same type, you should return 1.
This is wrong,
because when tourList.size() is zero, you will get this exception
Since you have only one "type" of list view item use this :
所以我不确定你想做什么,但要明白,当你在列表视图上调用 setAdapter 时, getViewTypeCount() 仅被调用一次。因此,如果此时列表为空或为空,您的应用程序将失败。这里返回的值应该被认为是一个常量。
ViewTypes 用于视图回收。基本上,通过将其指定为列表的长度,您可以说列表中的每个实体都无法重用列表中其他实体已使用的任何其他视图。如果确实如此,那么这种行为很好,但最小值必须始终为 1。此外,在调用 setAdapter 后,这不能更改,直到您创建一个新的适配器,并使用新内容在列表视图上设置(这是只是热列表适配器中的视图回收器起作用)。
我假设 viewtypeCount 为 1 就可以了,因为所有对象都属于同一类型。因此,不要重写 getItemViewType 或 getItemViewCount()。这些默认值分别为 0 和 1,这足以满足您发布的适配器代码的需要,并且仍然可以有效地使用视图回收器。
So I am not sure what you want to do, but understand that getViewTypeCount() is only called once when you call setAdapter on the list view. So if the list is null or empty at that point, your application will fail. The value returned here should be thought of as more of a constant.
ViewTypes are used in View Recycling. Basically by specifying it as the length of the list, you are saying that every entity in the list would not be able to reuse any other view that the others in the list have used. If this is really true then this behavior is fine, but the minimum value has to at all times be 1. Also this cannot change after calling setAdapter until you create a new adapter to be set on the list view with the new content (this is just hot the view recycler in lister adapter works).
I would assume you could get by with a viewtypeCount of 1 since all your objects are of the same type. So just don't override getItemViewType or getItemViewCount(). These default to 0 and 1 respectively which will suffice for the adapter code you posted and still make efficient use of the view recycler.
我知道这是一个老问题,但像我一样在这里徘徊的任何人都可以受益。你看,我需要使用:
因为这是我的行数据在滚动期间保持其位置的唯一方法。但我有时会遇到崩溃的问题。所以我添加了一些东西来防止崩溃,现在一切都很好:
I know it's an old question but anyone wandering here like i did can benefit. You see, i needed to use:
Because it was the only way my row data would maintain their position during scroll. But i faced the crashed issue at times. So i added something it to prevent crash and now i'm all good: