FutureMind / recycler-fast-scroll

Provides fast scroll and section idexer for recycler view
Apache License 2.0
492 stars 105 forks source link

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference #34

Closed ch-muhammad-adil closed 7 years ago

ch-muhammad-adil commented 7 years ago

I am facing this issue, Please assist me...

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference at com.futuremind.recyclerviewfastscroll.FastScroller.initHandleMovement(FastScroller.java:186) at com.futuremind.recyclerviewfastscroll.FastScroller.onLayout(FastScroller.java:162) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1767) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.support.design.widget.HeaderScrollingViewBehavior.layoutChild(HeaderScrollingViewBehavior.java:131) at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42) at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1364) at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:846) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1741) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585) at android.widget.LinearLayout.onLayout(LinearLayout.java:1494) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323) at android.widget.FrameLayout.onLayout(FrameLayout.java:261) at com.android.internal.policy.DecorView.onLayout(DecorView.java:724) at android.view.View.layout(View.java:17522) at android.view.ViewGroup.layout(ViewGroup.java:5612) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2342) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2069) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1246) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6301)

anudeepreddygopu commented 7 years ago

Hey, did you get any solution. I am facing the same issue. Thank you, Anudeep

ch-muhammad-adil commented 7 years ago

@anudeepreddygopu I have used another library.

ch-muhammad-adil commented 7 years ago

here you can have look . I have used its code https://github.com/AndroidDeveloperLB/LollipopContactsRecyclerViewFastScroller

micHar commented 7 years ago

Can you post your code that provoked this error? It's difficult to see what happened without the code.

Hitexroid commented 7 years ago

Hi , this is my problem too ! just in some device .

My Xml :

` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent">

<android.support.v7.widget.RecyclerView
    android:id="@+id/recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />

<com.futuremind.recyclerviewfastscroll.FastScroller
    android:id="@+id/fastscroll"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_alignParentRight="true"/>

<com.recyclerview.header.RecyclerViewHeader
    android:id="@+id/recyclerHeader"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"/>

`

when lunch app will crash

micHar commented 7 years ago

Thanks for XML, can you also post your Java code which relates to the issue?

ch-muhammad-adil commented 7 years ago

This is the fast scroller class just use this or create new one and copy past its code

https://github.com/AndroidDeveloperLB/LollipopContactsRecyclerViewFastScroller/blob/master/library/src/main/java/com/lb/recyclerview_fast_scroller/RecyclerViewFastScroller.java

Rest implementation you will find here https://github.com/AndroidDeveloperLB/LollipopContactsRecyclerViewFastScroller/tree/master/app/src/main

Just have a look on its java code and also on its resource files inside layout and drawables, you just need to copy paste those files.

Here you will see how he has used that class from the library so you can try this https://github.com/AndroidDeveloperLB/LollipopContactsRecyclerViewFastScroller/blob/master/app/src/main/res/layout/fragment_recycler_view.xml

Please note that he has used scroller class in the fragment_recycler_view.xml . you can see this code <com.lb.recyclerview_fast_scroller.RecyclerViewFastScroller android:id="@+id/fastscroller" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_alignBottom="@id/recyclerview" android:layout_alignParentEnd="true" android:layout_alignParentRight="true" android:layout_alignTop="@id/recyclerview" />

you need to change this com.lb.recyclerview_fast_scroller.RecyclerViewFastScroller to YourPackageName.RecyclerViewFastScroller . I hope you have got me know.

All you need to do is create your own class of RecyclerViewFastScroller and use it like I have explained and like the developer has used in sample app.

Hitexroid commented 7 years ago

@micHar

relative = (RelativeLayout) LayoutInflater.from(context).inflate(R.layout.recyclerandfastscroll1, null);

... other code to setContentView

recyclerView = (RecyclerView) relative.findViewById(R.id.recyclerview); fastScroller = (FastScroller) relative.findViewById(R.id.fastscroll);

LinearLayoutManager myLayoutManager = new LinearLayoutManager(context); recyclerView.setLayoutManager(myLayoutManager); recyclerView.setAdapter(mAdapter);

fastScroller.setRecyclerView(recyclerView);

micHar commented 7 years ago

@Hitexroid you don't setAdapter before setRecyclerView. See the readme.

    recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
    fastScroller = (FastScroller) findViewById(R.id.fastscroll);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adapter);

    //has to be called AFTER RecyclerView.setAdapter()
    fastScroller.setRecyclerView(recyclerView);
Hitexroid commented 7 years ago

no , i called setRecyclerView after setAdapter

and i said "crash in some device , not all device" in may xml post .

micHar commented 7 years ago

OK, I did not notice because of the strange formatting.

On what devices does it crash then? Manufacturer, Android version etc. Can you repeat it consistently on an emulator?

Shyri commented 7 years ago

It was happening to me also.

Digging in the code I found that in FastScroller's onLayout method, some operations depending on viewProvider are being done. Since you cannot control when or how many times onLayout will be called, you cannot be sure viewProvider will not be null.

The stacktrace indicates handle is null when it is trying to setOnTouchListener to it, but the root of the problem is that inside onLayout, initHandleMovement() is being called without any check if handle was set.

I could fix it with this workaround, checking if viewProvider is not null, but I'm not sure this a proper solution

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);

        if (viewProvider == null) {
            return;
        }

        initHandleMovement();
        bubbleOffset = viewProvider.getBubbleOffset();

        applyStyling(); //TODO this doesn't belong here, even if it works

        //sometimes recycler starts with a defined scroll (e.g. when coming from saved state)
        scrollListener.updateHandlePosition(recyclerView);

    }
micHar commented 7 years ago

The question is how is the viewProvider null there. onLayout is not called before the view started inflating, viewProvider should be already there by that time, I suppose.

micHar commented 7 years ago

I mean, what you wrote would be a good quickfix, but I would rather understand the issue and fix it properly.

micHar commented 7 years ago

Alright, the viewProvider was being set at latest inside setRecyclerView(recyclerView);. So the onLayout could apparently be called earlier which caused NPE in some specific circumstances (specific devices? - I cannot repeat it).

Therefore, now I create the default viewprovider in the contructor, so there should be no way it's null in onLayout. I released new version 0.2.5. I'm closing it now, reopen if you still experience this issue. Thanks!

billrancho commented 7 years ago

@micHar I am still facing this issue on 0.2.5. //// java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.widget.RecyclerView.computeVerticalScrollOffset()' on a null object reference at com.futuremind.recyclerviewfastscroll.RecyclerViewScrollListener.updateHandlePosition(RecyclerViewScrollListener.java:47) at com.futuremind.recyclerviewfastscroll.FastScroller.onLayout(FastScroller.java:168) ////

ayush9 commented 6 years ago

Same Error: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v7.widget.RecyclerView.computeVerticalScrollOffset()' on a null object reference

arjun902 commented 6 years ago

Hello everyone, I am facing this error ... please help me

01-14 00:43:10.424 4486-4486/? E/libprocessgroup: failed to make and chown /acct/uid_10058: Read-only file system 01-14 00:43:25.937 4486-4486/com.example.user.myapplication E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.user.myapplication, PID: 4486 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.user.myapplication/com.example.user.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) at android.app.ActivityThread.access$800(ActivityThread.java:151) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) 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) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference at com.example.user.myapplication.MainActivity.onCreate(MainActivity.java:56) at android.app.Activity.performCreate(Activity.java:5990) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)  at android.app.ActivityThread.access$800(ActivityThread.java:151)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  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) 

My Java Code is here

package com.example.user.myapplication;

import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast;

public class MainActivity extends AppCompatActivity { EditText num1, num2; TextView result; Button add, subtract, multiply, divide;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    num1 = findViewById(R.id.number1);
    num2 = findViewById(R.id.number2);
    result = findViewById(R.id.Result);
    add = findViewById(R.id.buttonAddAdd);
    subtract = findViewById(R.id.buttonSubtract);
    add = findViewById(R.id.buttonMultiply);
    add = findViewById(R.id.buttonDivide);

    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (num1.getText() != null && num2.getText() != null) {
                double n1 = Double.parseDouble(num1.getText().toString());
                double n2 = Double.parseDouble(num2.getText().toString());
                double res = n1 + n2;
                result.setText(String.valueOf(res));
            } else {
                Toast.makeText(view.getContext(), "please enter the number properly", Toast.LENGTH_SHORT).show();

            }
        }
    });
    subtract.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (num1.getText() != null && num2.getText() != null) {
                double n1 = Double.parseDouble(num1.getText().toString());
                double n2 = Double.parseDouble(num2.getText().toString());
                double res = n1 - n2;
                result.setText(String.valueOf(res));
            } else {
                Toast.makeText(view.getContext(), "please enter the number properly", Toast.LENGTH_SHORT).show();

            }
        }
    });
    multiply.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (num1.getText() != null && num2.getText() != null) {
                double n1 = Double.parseDouble(num1.getText().toString());
                double n2 = Double.parseDouble(num2.getText().toString());
                double res = n1 * n2;
                result.setText(String.valueOf(res));
            } else {
                Toast.makeText(view.getContext(), "please enter the number properly", Toast.LENGTH_SHORT).show();

            }
        }
    });
    divide.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (num1.getText() != null && num2.getText() != null) {
                double n1 = Double.parseDouble(num1.getText().toString());
                double n2 = Double.parseDouble(num2.getText().toString());
                double res = n1 / n2;
                result.setText(String.valueOf(res));
            } else {
                Toast.makeText(view.getContext(), "please enter the number properly", Toast.LENGTH_SHORT).show();
            }
        }
    });
}

}

fitnessthetic commented 6 years ago

same problem.. image

kalyanthirupathi commented 5 years ago

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView$LayoutManager.smoothScrollToPosition(android.support.v7.widget.RecyclerView, android.support.v7.widget.RecyclerView$State, int)' on a null object reference

getting this error can anyone slove this

tgvdinesh commented 5 years ago

on a null object reference. When you try to do a method call over a null object then you'll get the error.

In most of the case findViewByID will return null if you don't call setContextView before your findViewByID call.

altotunchitoo commented 3 years ago
java.lang.NullPointerException: Attempt to invoke virtual method 'int androidx.recyclerview.widget.RecyclerView.computeVerticalScrollOffset()' on a null object reference
    at com.futuremind.recyclerviewfastscroll.RecyclerViewScrollListener.updateHandlePosition(RecyclerViewScrollListener.java:47)

I'm facing the same error when there are no rows in the recycler view. :-)