嗨我有自定义适配器的列表视图...一个图像和两个文本视图....当我向上滚动活动崩溃时... listview被片段附着,片段被活动附着... ...
logcat是
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.cuelearn.cuemathteacher.adapters.StudentsEnrollDetailCustomAdapter.getView(StudentsEnrollDetailCustomAdapter.java:64)
at android.widget.AbsListView.obtainView(AbsListView.java:2255)
at android.widget.ListView.makeAndAddView(ListView.java:1769)
at android.widget.ListView.fillUp(ListView.java:706)
at android.widget.ListView.fillGap(ListView.java:645)
at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5040)
at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3197)
at android.widget.AbsListView.onTouchEvent(AbsListView.java:3471)
at android.view.View.dispatchTouchEvent(View.java:7127)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2170)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1905)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2176)
at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1919)
at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1925)
at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1379)
at android.app.Activity.dispatchTouchEvent(Activity.java:2396)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1873)
at android.view.View.dispatchPointerEvent(View.java:7307)
at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3174)
at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3119)
at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4155)
at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4134)
at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4226)
at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:171)
at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:163)
at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4205)
at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4245)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:523)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是自定义适配器文件
public class StudentsEnrollDetailCustomAdapter extends ArrayAdapter<StudentInfo> {
Context context;
StudentInfo[] arr_StudentInfo;
static class ViewHolder{
TextView tv_student_name,tv_student_class;
ImageView iv_student_photo;
}
public StudentsEnrollDetailCustomAdapter(Context context,StudentInfo[] arr_StudentInfo) {
super(context, R.layout.adapter_students_enroll_detail, arr_StudentInfo);
this.context = context;
this.arr_StudentInfo = arr_StudentInfo;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if(rowView == null){
rowView = ((Activity)context).getLayoutInflater().inflate(R.layout.adapter_students_enroll_detail, null);
ViewHolder viewHolder = new ViewHolder();
viewHolder.tv_student_name = (TextView)rowView.findViewById(R.id.firstLine);
viewHolder.tv_student_class = (TextView)rowView.findViewById(R.id.secondLine);
viewHolder.iv_student_photo = (ImageView)rowView.findViewById(R.id.icon);
rowView.setTag(viewHolder);
}
ViewHolder holder = (ViewHolder) rowView.getTag();
StudentInfo s = arr_StudentInfo[position];
if(s.getStudentStatus() == 0){
holder.tv_student_name.setText(""+s.getStudentName());
holder.tv_student_class.setText("Class : "+s.getStudentClass());
if(s.getStudentImage() == null){
holder.iv_student_photo.setImageResource(R.drawable.ic_contact_picture);
}else{
holder.iv_student_photo.setImageBitmap(s.getStudentImage());
}
CUtils.setCustomFontNormal(context, holder.tv_student_name, CConstants.FONT_F3);
CUtils.setCustomFontNormal(context, holder.tv_student_class, CConstants.FONT_F4);
}else{
rowView = ((Activity)context).getLayoutInflater().inflate(R.layout.row_null, null);
}
return rowView;
}
}
错误显示在此自定义适配器行中,但没有问题..
holder.tv_student_name.setText(""+s.getStudentName());
答案 0 :(得分:0)
由于某种原因,s为null。 这意味着arr_StudentInfo [position]为null。 反过来,这可能意味着您尚未在该位置初始化数组元素。 HTH。
答案 1 :(得分:0)
检查StudentInfor数组是否填写正确。根据例外,一个问题是位置明智地获取StudentInfor值。
第二:getView
方法实现错误。
因为当这个(s.getStudentStatus())方法没有返回0时 ..我必须从列表视图中隐藏该项目...并且第二次膨胀 布局没有内容......只包含linearlayout 没有任何孩子
请勿在{{1}}中添加您在Adapter中传递的列表行项。删除或过滤后传递并显示
在下面发布的代码我使用StudentInfo[] arr_StudentInfo
作为使用适配器进行自定义列表视图的最佳选择
BaseAdapter