0
我使用listview与baseadapter和滚动的listview它崩溃。Baseadapter与ViewHolder,在Scroll上崩溃
我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)
覆盖'getViewTypeCount',使其返回2,然后再次运行它 – Blackbelt
非常感谢@Blackbelt,它现在工作正常 ,我也必须只实现上述方法或任何东西? –
我不知道。当然,你必须实现BaseAdapter的抽象方法。对于其他人是由你决定的。你有特定的问题吗? – Blackbelt