我正在使用带有baseadapter的listview,并且在列表视图滚动时它会崩溃。
我getView
类的BaseAdapter
方法
@Override
public View getView(final int arg0, View arg1, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = activity.getLayoutInflater();
ViewHolder holder=null;
if(arg1 == null){
//Login category
if(categoryList.get(arg0).getType()==1){
arg1 = inflater.inflate(R.layout.switch_profile_header, null);
holder=new ViewHolder();
holder.layout=(RelativeLayout)arg1.findViewById(R.id.switch_profile_header_view);
holder.categoryName =(TextView)arg1.findViewById(R.id.categoryName);
holder.isLogin=categoryList.get(arg0).getIsLogin();
arg1.setTag(holder);
}
//Other category
else if(categoryList.get(arg0).getType()==0){
arg1 = inflater.inflate(R.layout.switch_profile_listrow, null);
holder=new ViewHolder();
holder.layout=(RelativeLayout)arg1.findViewById(R.id.switch_profile_listrow_view);
holder.categoryName =(TextView)arg1.findViewById(R.id.categoryName);
holder.isLogin=categoryList.get(arg0).getIsLogin();
holder.isDelete=(ImageView)arg1.findViewById(R.id.categoryDelete);
holder.login=(ImageView)arg1.findViewById(R.id.categoryLogin);
arg1.setTag(holder);
}
}else {
holder = (ViewHolder) arg1.getTag();
}
//Login category
if(categoryList.get(arg0).getType()==1){
holder.categoryName.setText(Html.fromHtml(categoryList.get(arg0).getCategoryName()));
holder.layout.setBackgroundColor(Color.parseColor("#efefef"));
}
//Login category
else if(categoryList.get(arg0).getType()==0){
if(flag==false){
holder.isDelete.setVisibility(View.INVISIBLE);
}else{
holder.isDelete.setVisibility(View.VISIBLE);
}
if(categoryList.get(arg0).getIsLogin().equalsIgnoreCase("1")){
holder.isDelete.setVisibility(View.INVISIBLE);
holder.login.setVisibility(View.INVISIBLE);
}else if(flag) {
holder.isDelete.setVisibility(View.VISIBLE);
holder.login.setVisibility(View.VISIBLE);
}
holder.categoryName.setText(Html.fromHtml(categoryList.get(arg0).getCategoryName()));
if(categoryList.get(arg0).getIsLogin().equalsIgnoreCase("1")){
holder.layout.setBackgroundColor(Color.parseColor("#efefef"));
}else{
holder.layout.setBackgroundColor(Color.parseColor("#f0f0f0"));
}
return arg1;
}
我的Logcat
Process: com.Tiger.Tiger, PID: 14059
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference
at com.TenderTiger.Adapter.SwitchProfileAdapter.getView(SwitchProfileAdapter.java:117)
at android.widget.HeaderViewListAdapter.getView(HeaderViewListAdapter.java:220)
at android.widget.AbsListView.obtainView(AbsListView.java:2344)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.correctTooLow(ListView.java:1474)
答案 0 :(得分:0)
因为我必须在getView
方法中夸大两种不同的布局。
正如Blackbelt所建议的那样,我必须在getViewTypeCount
类中实现BaseAdapter
方法。
我做了上面的工作并且工作正常,但是当我滚动它会强制关闭。
我们必须实施
getViewTypeCount
以及getItemViewType
将其付诸实践
1)getViewTypeCount
方法返回布局总数
2)getItemViewType
方法返回哪个行以适当的条件膨胀。
所以最后我的代码看起来像这个
private static final int TYPE_HEADER = 0;
private static final int TYPE_ROW = 1;
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
return (categoryList.get(position).getType()==1)?TYPE_HEADER :TYPE_ROW;
}
最后是我的getView
方法
@Override
public View getView(final int position, View convertView, ViewGroup arg2) {
// TODO Auto-generated method stub
LayoutInflater inflater = activity.getLayoutInflater();
ViewHolder holder=null;
if(convertView == null) {
if (getItemViewType(position) == TYPE_HEADER) { // Header
convertView = inflater.inflate(R.layout.switch_profile_header, null);
holder = new ViewHolder();
holder.layout = (RelativeLayout) convertView.findViewById(R.id.switch_profile_header_view);
holder.categoryName = (TextView) convertView.findViewById(R.id.categoryName);
holder.isLogin = categoryList.get(position).getIsLogin();
convertView.setTag(holder);
} else {
//row
convertView = inflater.inflate(R.layout.switch_profile_listrow, null);
holder = new ViewHolder();
holder.layout = (RelativeLayout) convertView.findViewById(R.id.switch_profile_listrow_view);
holder.categoryName = (TextView) convertView.findViewById(R.id.categoryName);
holder.isLogin = categoryList.get(position).getIsLogin();
holder.isDelete = (ImageView) convertView.findViewById(R.id.categoryDelete);
holder.login = (ImageView) convertView.findViewById(R.id.categoryLogin);
convertView.setTag(holder);
}
}else {
holder = (ViewHolder) convertView.getTag();
}
//Header
if(getItemViewType(position) == TYPE_HEADER){
}
//row
else if(getItemViewType(position) == TYPE_ROW){
}
return convertView;
}