0

I implemented a custom listview, with 4 different custom rows. when i display all rows aut one screen every things work. But when I e.g. switch the screen into horizontal format, then the listview have to scroll. When I scroll down and the first item on the top disappears, the app crashes!

Here is the code in the getView method:

    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;
}

(Not all of the custom rows have a image)

This is the crash report:

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)

For help I would be very grateful!

mmlooloo
  • 18,937
  • 5
  • 45
  • 64
mkersche17
  • 105
  • 13

2 Answers2

0

When you switch your screen position, the view is generated again. If you try to use some reference to an old view, it will not exist any more, causing the NullPointerException you got there.

Check this another answer for further explanation on how to handle screen rotation.

Community
  • 1
  • 1
Logain
  • 4,259
  • 1
  • 23
  • 32
  • But it also happen when I scroll in portrait mode. I had increase the row hight, so I also have to scroll in portait and here is the same issue. When the first row disappears, the app crashes. – mkersche17 Oct 05 '14 at 21:09
  • @mmlooloo has a good point on his answer. Anyway, I would ask you which line is `SidebarListAdapter.java:125`, just to be sure. – Logain Oct 05 '14 at 21:16
  • @AlvarezAriel my answer is not good, the accepted answer is better see that. – mmlooloo Oct 06 '14 at 13:28
0

When you want to use more than one layout in the Adapter, there is two important methods that need to be overriden :

  • getViewTypeCount : must return the number of different layouts used by the Adapter

  • getItemViewType : must return an int between 0 and getViewTypeCount - 1

In your case, you need to add

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

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

With those methods implemented, the convertView will always have the right type and you won't have any trouble finding your views.

NB : To improve performance, add a ViewHolder as a tag to your views

Julien Arzul
  • 961
  • 9
  • 7