android - 滚动后第一个项目消失后自定义列表视图崩溃

时间:2014-10-05 20:23:31

标签: java android listview crash

我实现了一个自定义列表视图,包含4个不同的自定义行。当我在一个屏幕上显示所有行时,每件事都有效。但是,当我将屏幕切换为横向格式,然后列表视图必须滚动。 当我向下滚动并且顶部的第一个项目消失时,应用程序崩溃了!

以下是getView方法中的代码:

    View row = null;


    if(convertView==null){

        LayoutInflater inflater = LayoutInflater.from(context);

        switch (rowFinder[position]){
        case 1: 
                row = inflater.inflate(R.layout.header_sidebar_row, parent, false);
                break;

        case 2: 
                row = inflater.inflate(R.layout.current_club, parent, false);
                break;

        case 3: 
                row = inflater.inflate(R.layout.custom_sidebar_row, parent, false);
                break;
        case 4:
                row = inflater.inflate(R.layout.section_sidebar_header, parent, false);
                break;
        }

    }
    else{

        row = convertView;
    }


    TextView sidebarTitle;
    ImageView iconImage = null;

    switch (rowFinder[position]){
    case 1: 
        sidebarTitle =(TextView) row.findViewById(R.id.user_name);
        iconImage = (ImageView) row.findViewById(R.id.user_image);  
        break;
    case 2:
        sidebarTitle =(TextView) row.findViewById(R.id.club_name);
        iconImage = (ImageView) row.findViewById(R.id.club_image);
        break;
    case 3:
        sidebarTitle =(TextView) row.findViewById(R.id.menu_item);
        iconImage = (ImageView) row.findViewById(R.id.icon_image);  
        break;
    case 4:
        sidebarTitle =(TextView) row.findViewById(R.id.section_item);
        iconImage = null;
        break;

    default:
        sidebarTitle = (TextView) row.findViewById(R.id.menu_item);
        iconImage = null;
        break;

    }

    sidebarTitle.setText(titlesSideMenu[position]);
    if(iconImage != null){
        iconImage.setImageResource(images[position]);
    }

    return row;
}

(并非所有自定义行都有图像)

这是崩溃报告:

10-05 20:02:08.710: E/InputEventReceiver(2126): Exception dispatching input event.
10-05 20:02:08.730: D/AndroidRuntime(2126): Shutting down VM
10-05 20:02:08.743: W/dalvikvm(2126): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
10-05 20:02:08.800: E/AndroidRuntime(2126): FATAL EXCEPTION: main
10-05 20:02:08.800: E/AndroidRuntime(2126): java.lang.NullPointerException
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.example.rivler.SidebarListAdapter.getView(SidebarListAdapter.java:125)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.AbsListView.obtainView(AbsListView.java:2143)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.ListView.makeAndAddView(ListView.java:1831)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.ListView.fillDown(ListView.java:674)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.ListView.fillGap(ListView.java:638)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.AbsListView.trackMotionScroll(AbsListView.java:4930)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3087)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.widget.AbsListView.onTouchEvent(AbsListView.java:3361)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.View.dispatchTouchEvent(View.java:7246)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2168)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1903)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2174)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1917)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1953)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1405)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.app.Activity.dispatchTouchEvent(Activity.java:2410)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1901)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.View.dispatchPointerEvent(View.java:7426)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl.deliverPointerEvent(ViewRootImpl.java:3220)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:3165)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:4292)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:4271)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:4363)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:179)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:171)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:4342)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:4382)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.Choreographer.doFrame(Choreographer.java:530)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.os.Handler.handleCallback(Handler.java:725)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.os.Handler.dispatchMessage(Handler.java:92)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.os.Looper.loop(Looper.java:137)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at android.app.ActivityThread.main(ActivityThread.java:5041)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invokeNative(Native Method)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at java.lang.reflect.Method.invoke(Method.java:511)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
10-05 20:02:08.800: E/AndroidRuntime(2126):     at dalvik.system.NativeStart.main(Native Method)

求助,我将非常感激!

2 个答案:

答案 0 :(得分:0)

切换屏幕位置时,将再次生成视图。如果你尝试使用一些旧视图的引用,它将不再存在,导致你到达那里的NullPointerException。

检查this another answer以获取有关如何处理屏幕旋转的进一步说明。

答案 1 :(得分:0)

如果要在适配器中使用多个布局,有两个重要方法需要覆盖:

  • getViewTypeCount:必须返回适配器使用的不同布局的数量

  • getItemViewType:必须返回介于0和getViewTypeCount之间的int - 1

在您的情况下,您需要添加

@Override
public int getViewTypeCount() {
    return 4;
}

@Override
public int getItemViewType(int position) {
    return rowFinder[position] - 1;
}

通过实现这些方法,convertView将始终具有正确的类型,您将无法找到您的观点。

注意:要提高效果,请将ViewHolder添加为视图标记