Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Bug with ViewPager and RecyclerView

I'm using a ViewPager with an RecyclerView inside the first fragment of the ViewPager.

When I scroll left to right, no errors. But when I scroll right to left (from the #2 for #1 fragment of the ViewPager), the following error occurs:

05-10 20:21:07.546  20002-20002/com.graffiti75.android.worldremit E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.graffiti75.android.worldremit, PID: 20002
    java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference
            at android.support.v7.widget.RecyclerView.onInterceptTouchEvent(RecyclerView.java:1819)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:1960)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2405)
            at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2049)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2369)
            at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1719)
            at android.app.Activity.dispatchTouchEvent(Activity.java:2742)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2330)
            at android.view.View.dispatchPointerEvent(View.java:8666)
            at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4123)
            at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3989)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
            at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3680)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
            at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3737)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3597)
            at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3563)
            at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3571)
            at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3544)
            at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5807)
            at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5781)
            at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5752)
            at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5897)
            at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
            at android.os.MessageQueue.nativePollOnce(Native Method)
            at android.os.MessageQueue.next(MessageQueue.java:143)
            at android.os.Looper.loop(Looper.java:122)
            at android.app.ActivityThread.main(ActivityThread.java:5254)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-10 20:21:07.549     550-7690/? W/ActivityManager﹕ Force finishing activity 1 com.graffiti75.android.worldremit/.activity.WalkthroughActivity

That's the error: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.support.v7.widget.RecyclerView$LayoutManager.canScrollHorizontally()' on a null object reference

The method below is the First Fragment of my ViewPager. When I comment the lines where the RecyclerView is called, no errors occur. But I need this RecyclerView to browse my contacts! So I guess this proves the error happens in those lines...

public class ContactListFragment extends Fragment {

    //--------------------------------------------------
    // Constructor
    //--------------------------------------------------

    public static ContactListFragment newInstance() {
        ContactListFragment fragment = new ContactListFragment();
        Bundle args = new Bundle();
        fragment.setArguments(args);
        return fragment;
    }

    public ContactListFragment() {
        // Required empty public constructor
    }

    //--------------------------------------------------
    // Fragment Life Cycle
    //--------------------------------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment.
        View v = inflater.inflate(R.layout.fragment_contact_list, container, false);

        // Get data.
        List<String> nameList = new ArrayList<String>();
        Cursor cursor = getContacts();
        while (cursor.moveToNext()) {
            String name = cursor.getString(cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
            nameList.add(name);
        }

        // Setup list.
        RecyclerView recyclerView = (RecyclerView) v.findViewById(R.id.id_recycler_view);
        recyclerView.setHasFixedSize(true);

        Activity activity = getActivity();
        final LinearLayoutManager layoutManager = new LinearLayoutManager(activity);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(layoutManager);

        recyclerView.setItemAnimator(new DefaultItemAnimator());
        recyclerView.setAdapter(new ContactListAdapter(getActivity(), nameList));

        return v;
    }

    //--------------------------------------------------
    // Methods
    //--------------------------------------------------

    private Cursor getContacts() {
        // Run query.
        Uri uri = ContactsContract.Contacts.CONTENT_URI;
        String[] projection = new String[] { ContactsContract.Contacts._ID,  ContactsContract.Contacts.DISPLAY_NAME };
        String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '" + ("1") + "'";
        String[] selectionArgs = null;
        //String sortOrder = ContactsContract.Contacts._ID + " COLLATE LOCALIZED ASC";
        String sortOrder = ContactsContract.Contacts.DISPLAY_NAME + " COLLATE LOCALIZED ASC";

        return getActivity().managedQuery(uri, projection, selection, selectionArgs, sortOrder);
    }
}

Those line below: // Setup list.

I researched A LOT in Google and I couldn't find a way to solve this errors.

Does anybody has a guess?

like image 892
graffiti75 Avatar asked May 10 '15 20:05

graffiti75


1 Answers

I had the same issue, I created subclass from ViewPager, which handles this error. I hope this will help you.

public class HackyViewPager extends ViewPager {


public HackyViewPager(Context context) {
    super(context);
}

public HackyViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (Throwable t) {
        t.printStackTrace();
        return false;
    }
}}
like image 154
Nazar Ivanchuk Avatar answered Oct 30 '22 03:10

Nazar Ivanchuk