我创建了自己的适配器,将一些音乐会放在列表中。没有day分隔符,一切正常,但是当它们向下滚动列表并且为屏幕上显示的每个新行调用convertView函数时,我会在convertView函数的以下行中获得NullPointer异常:
tvNombre.setText(entry.getArtista());
但是我可以在日志中打印艺术家的名字,我修改后将正确的列表传递给适配器。有人能帮助我吗?
提前致谢。
listConciertoEvento.add(new ConciertoEvento("dia_0", "", new GregorianCalendar(2012,10,dia,0,0)));
listConciertoEvento.add(new ConciertoEvento(x++,"Moodyman","Becool","Electrónica",
new GregorianCalendar(2012,10,dia,23,30),new GregorianCalendar(2012,10,dia+1,5,0),"","",10));
listConciertoEvento.add(new ConciertoEvento(x++,"Chinese Man","Razzmataz 2","Trip-Hop",
new GregorianCalendar(2012,10,dia,21,0),new GregorianCalendar(2012,10,dia,23,0),"","", 50));
listConciertoEvento.add(new ConciertoEvento(x++,"Kreator","Razzmataz","Thrash Metal",
new GregorianCalendar(2012,10,dia,20,30),new GregorianCalendar(2012,10,dia,23,0),"","",10));
listConciertoEvento.add(new ConciertoEvento(x++,"Barricada","KGB","Rock",
new GregorianCalendar(2012,10,dia,22,0),new GregorianCalendar(2012,10,dia+1,0,30),"","", 50));
listConciertoEvento.add(new ConciertoEvento(x++,"The Buzzcocks","Apolo","Thrash Metal",
new GregorianCalendar(2012,10,dia,21,30),new GregorianCalendar(2012,10,dia,23,30),"","",10));
listConciertoEvento.add(new ConciertoEvento(x++,"Mr.Oizo","La 2","Electrónica",
new GregorianCalendar(2012,10,dia,23,30),new GregorianCalendar(2012,10,dia+1,3,0),"","", 50));
dia++;
//listConciertoEvento.add(new ConciertoEvento("dia_1", "", new GregorianCalendar(2012,10,dia,0,0)));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_1","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_2","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_3","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_4","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_5","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_6","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_7","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_8","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_9","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_10","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_11","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_12","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_13","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_14","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
listConciertoEvento.add(new ConciertoEvento(x++,"Cannibal Corpse_15","Razzmataz","Death Metal",new GregorianCalendar(2012,10,13,13,30),new GregorianCalendar(2012,10,13,15,0),"","", 15));
这里是适配器上convertView的代码:
public View getView(int position, View convertView, ViewGroup viewGroup) {
ConciertoEvento entry = listConciertoEvento.get(position);
if (convertView == null) {
if (!entry.getArtista().startsWith("dia_")){
Log.v("lalalala", "XXXX layout concierto");
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.concierto_evento_row, null);
}else{
Log.v("lalalala", "XXXX layout dia");
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.dia_row, null);
}
}
final int id = entry.getId();
if (!entry.getArtista().startsWith("dia_")){
Log.v("lalalala", "XXXX is concert");
ImageView imSquare = (ImageView) convertView.findViewById(R.id.imSquare);
TextView tvNombre = (TextView) convertView.findViewById(R.id.tvNombre);
Log.v("lalalala", entry.getArtista());
tvNombre.setText(entry.getArtista());
Log.v("lalalala", "XXXX tvNombre.setText(entry.getArtista()); done");
TextView tvPath = (TextView) convertView.findViewById(R.id.tvPath);
tvPath.setText(entry.getPath_imagen());
}else{
Log.v("lalalala", "XXXX is day");
Log.v("lalalala", entry.getArtista());
}
return convertView;
}
这里是日志:
10-22 12:23:11.468: V/lalalala(582): XXXXXXXXXXX xivato en fragment2
10-22 12:23:11.468: V/lalalala(582): dia_0
10-22 12:23:11.468: V/lalalala(582): Kreator
10-22 12:23:11.468: V/lalalala(582): Chinese Man
10-22 12:23:11.468: V/lalalala(582): The Buzzcocks
10-22 12:23:11.468: V/lalalala(582): Barricada
10-22 12:23:11.468: V/lalalala(582): Mr.Oizo
10-22 12:23:11.468: V/lalalala(582): Moodyman
10-22 12:23:11.468: V/lalalala(582): dia_1
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_15
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_14
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_13
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_12
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_11
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_10
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_9
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_8
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_7
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_6
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_5
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_4
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_3
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_2
10-22 12:23:11.468: V/lalalala(582): Cannibal Corpse_1
10-22 12:23:11.619: D/dalvikvm(582): GC_EXTERNAL_ALLOC freed 130K, 51% free 2642K/5379K, external 904K/1038K, paused 48ms
10-22 12:23:11.669: V/lalalala(582): XXXX layout dia
10-22 12:23:11.669: V/lalalala(582): XXXX is day
10-22 12:23:11.669: V/lalalala(582): dia_0
10-22 12:23:11.679: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.739: D/dalvikvm(582): GC_EXTERNAL_ALLOC freed 12K, 51% free 2640K/5379K, external 1058K/1570K, paused 50ms
10-22 12:23:11.779: V/lalalala(582): XXXX is concert
10-22 12:23:11.779: V/lalalala(582): Kreator
10-22 12:23:11.779: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.789: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.799: V/lalalala(582): XXXX is concert
10-22 12:23:11.799: V/lalalala(582): Chinese Man
10-22 12:23:11.799: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.809: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.819: V/lalalala(582): XXXX is concert
10-22 12:23:11.819: V/lalalala(582): The Buzzcocks
10-22 12:23:11.819: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.819: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.839: V/lalalala(582): XXXX is concert
10-22 12:23:11.839: V/lalalala(582): Barricada
10-22 12:23:11.839: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.839: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.869: V/lalalala(582): XXXX is concert
10-22 12:23:11.879: V/lalalala(582): Mr.Oizo
10-22 12:23:11.879: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.889: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.899: V/lalalala(582): XXXX is concert
10-22 12:23:11.899: V/lalalala(582): Moodyman
10-22 12:23:11.899: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:11.909: V/lalalala(582): XXXX layout dia
10-22 12:23:11.909: V/lalalala(582): XXXX is day
10-22 12:23:11.909: V/lalalala(582): dia_1
10-22 12:23:11.909: V/lalalala(582): XXXX layout concierto
10-22 12:23:11.929: V/lalalala(582): XXXX is concert
10-22 12:23:11.929: V/lalalala(582): Cannibal Corpse_15
10-22 12:23:11.929: V/lalalala(582): XXXX tvNombre.setText(entry.getArtista()); done
10-22 12:23:37.829: W/KeyCharacterMap(582): No keyboard for id 0
10-22 12:23:37.829: W/KeyCharacterMap(582): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
10-22 12:23:39.729: V/lalalala(582): XXXX is concert
10-22 12:23:39.739: V/lalalala(582): Cannibal Corpse_14
10-22 12:23:39.739: D/AndroidRuntime(582): Shutting down VM
10-22 12:23:39.749: W/dalvikvm(582): threadid=1: thread exiting with uncaught exception (group=0x40015560)
10-22 12:23:39.769: E/AndroidRuntime(582): FATAL EXCEPTION: main
10-22 12:23:39.769: E/AndroidRuntime(582): java.lang.NullPointerException
10-22 12:23:39.769: E/AndroidRuntime(582): at com.u.totsound.AdapterConciertoEvento.getView(AdapterConciertoEvento.java:89)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.AbsListView.obtainView(AbsListView.java:1409)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.addViewBelow(ListView.java:2914)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.scrollListItemsBy(ListView.java:2843)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.arrowScrollImpl(ListView.java:2340)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.arrowScroll(ListView.java:2287)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.commonKey(ListView.java:2089)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.onKeyDown(ListView.java:2036)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.View.dispatchKeyEvent(View.java:3855)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:787)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.ListView.dispatchKeyEvent(ListView.java:2021)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.widget.TabHost.dispatchKeyEvent(TabHost.java:278)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:789)
10-22 12:23:39.769: E/AndroidRuntime(582): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchKeyEvent(PhoneWindow.java:1687)
10-22 12:23:39.769: E/AndroidRuntime(582): at com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1120)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.app.Activity.dispatchKeyEvent(Activity.java:2073)
10-22 12:23:39.769: E/AndroidRuntime(582): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1663)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2560)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2535)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.view.ViewRoot.handleMessage(ViewRoot.java:1867)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.os.Handler.dispatchMessage(Handler.java:99)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.os.Looper.loop(Looper.java:123)
10-22 12:23:39.769: E/AndroidRuntime(582): at android.app.ActivityThread.main(ActivityThread.java:3683)
10-22 12:23:39.769: E/AndroidRuntime(582): at java.lang.reflect.Method.invokeNative(Native Method)
10-22 12:23:39.769: E/AndroidRuntime(582): at java.lang.reflect.Method.invoke(Method.java:507)
10-22 12:23:39.769: E/AndroidRuntime(582): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-22 12:23:39.769: E/AndroidRuntime(582): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-22 12:23:39.769: E/AndroidRuntime(582): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你知道ListAdapter有办法管理不同类型的行吗?
您应该依赖public int getItemViewType (int position)
和public int getViewTypeCount ()
而不是检查entry.getArtist()
是否以"dia"
开头。这个问题肯定与此有关。