我有一个使用该应用程序的可扩展列表的问题.
当我开始显示/隐藏不同的组时,我的列表会引发错误.然后他给我一个错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
我不知道为什么它显示我这个错误,因为如果我的convertView没有持有人,那么它创建新的:(
首先,它使用List_Kategoria文件来存储类别.
其次,我有一些代表几种布局的列表.它们被输入该类别.
以下是可扩展列表视图的一些方法.
@Override
public Object getChild(int groupPosition, int childPosition) {
ArrayList lista = catList.get(groupPosition).getItemList();
Object obj = lista.get(childPosition);
return obj;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
ArrayList lista = catList.get(groupPosition).getItemList();
Object obj = lista.get(childPosition);
return obj.hashCode();
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
ViewHolder holder = null;
ArrayList lista = catList.get(groupPosition).getItemList();
Object obj = lista.get(childPosition);
if(convertView != null) {
holder = (ViewHolder) convertView.getTag();
}
if (convertView == null || convertView.getTag()==null) {
holder = new ViewHolder();
if(obj instanceof List_item_alarm_4 ){
convertView = inflater.inflate(R.layout.list_item_exp_alarm,null);
}
if(obj instanceof List_item_wyswietl_0){
convertView = inflater.inflate(R.layout.list_item_exp_temp,null);
}
if(obj instanceof List_item_tempzad_1){
convertView = inflater.inflate(R.layout.list_item_exp_temp_zad,null);
}
if(obj instanceof List_item_went_wyswietl){
convertView = inflater.inflate(R.layout.list_item_exp_went_wyswietl,null);
}
if(obj instanceof List_item_went_zad){
convertView = inflater.inflate(R.layout.list_item_exp_went_zad,null);
}
convertView.setTag(holder);
}
if(obj instanceof List_item_alarm_4 ) {
holder.item_alarm = (TextView) convertView.findViewById(R.id.item_alarm);
holder.item_alarm.setText("asd");
}
if(obj instanceof List_item_wyswietl_0){
holder.item_temp_0_nazwa = (TextView) convertView.findViewById(R.id.item_temp_0_nazwa);
holder.item_temp_0_wartosc = (TextView) convertView.findViewById(R.id.item_temp_0_wartosc);
holder.item_temp_0_nazwa.setText("temp");
holder.item_temp_0_wartosc.setText("XX");
}
if(obj instanceof List_item_tempzad_1){
holder.item_tempzad_1_nazwa = (TextView) convertView.findViewById(R.id.item_tempzad_1_nazwa);
holder.item_tempzad_1_nazwa.setText("conv");
}
if(obj instanceof List_item_went_wyswietl){
holder.tv_wentRodzaj = (TextView) convertView.findViewById(R.id.tV_went_rodzaj);
holder.tv_wentPrad = (TextView) convertView.findViewById(R.id.tV_wentPrad);
holder.tv_wentCzest = (TextView) convertView.findViewById(R.id.tV_wentCzest);
holder.tv_wentRodzaj.setText("asd");
holder.tv_wentPrad.setText("asd");
holder.tv_wentCzest.setText("asd");
}
if(obj instanceof List_item_went_zad){
holder.tV_wentRodzajZad = (TextView) convertView.findViewById(R.id.tV_went_rodzaj_zad);
holder.eT_went_niski = (EditText) convertView.findViewById(R.id.eT_went_niski);
holder.eT_went_ekono = (EditText) convertView.findViewById(R.id.eT_went_ekono);
holder.eT_went_komfort = (EditText) convertView.findViewById(R.id.eT_went_komf);
holder.tV_wentRodzajZad.setText("NAWIEW");
holder.eT_went_niski.setText("10");
holder.eT_went_ekono.setText("10");
holder.eT_went_komfort.setText("10");
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
int size = catList.get(groupPosition).getItemList().size();
return size;
}
这是我用来初始化数据的方法
private void initData(){
catList = new ArrayList();
List_Kategoria cat1 = createCategory("NAME",0, "R.mipmap/ic_launcher");
cat1.setItemList(createItemsCat1());
catList.add(cat1);
List_Kategoria cat2 = createCategory("NAME",1, "R.mipmap/ic_launcher");
cat2.setItemList(createItemsCat2());
catList.add(cat2);
}
private List_Kategoria createCategory(String name, long id, String imgPath) {
return new List_Kategoria(id, name, imgPath);
}
private ArrayList createItemsCat1(){
ArrayList result = new ArrayList();
List_item_wyswietl_0 Dana1= new List_item_wyswietl_0(0, "Dana1", "xx.x");
List_item_wyswietl_0 Dana2 = new List_item_wyswietl_0(1, "Dana2", "xx.x");
List_item_tempzad_1 Dana3 = new List_item_tempzad_1(2, "Dana3", "xx.x");
result.add(Dana1);
result.add(Dana2);
result.add(Dana3);
return result;
}
private ArrayList createItemsCat2(){
ArrayList result = new ArrayList();
List_item_wyswietl_0 Dana1 = new List_item_wyswietl_0(0, "Dana1", "xxx");
result.add(Grzanie);
return result;
}
我再次检查了这个应用程序,如果我有3组或更多,我有错误,如果我有2组一切都OK 🙂
如果我有3个具有相同List_category的组,一切正常:(
如果我有组,哪一组有几个不同的列表元素,我有问题
为什么??? 🙁