FABProgressCircle icon indicating copy to clipboard operation
FABProgressCircle copied to clipboard

Null Pointer Exception when calling show() function

Open Foivos-Stamopoulos opened this issue 8 years ago • 8 comments

In the following code, if fabProgressCircle.show() is called outside the Handler (which delays it's excecution for 500 milliseconds), the App crashes with Null Pointer Exception for fabProgressCircle.show(). Why could this happen? Does the fabProgressCircle needs some time to instantiate?

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_2);

    com.melnykov.fab.FloatingActionButton doneButton = (com.melnykov.fab.FloatingActionButton) findViewById(R.id.done2);
    com.github.jorgecastilloprz.FABProgressCircle fabProgressCircle = (com.github.jorgecastilloprz.FABProgressCircle) findViewById(R.id.fabProgressCircle );


    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            fabProgressCircle .show();
        }
    }, 500);
}

Foivos-Stamopoulos avatar Apr 12 '16 14:04 Foivos-Stamopoulos

+1

thiagokimo avatar Apr 25 '16 15:04 thiagokimo

In my case, addArcView() is not being called.

thiagokimo avatar Apr 25 '16 15:04 thiagokimo

+1, addArcView() is not being called.

mstedler avatar Aug 13 '16 16:08 mstedler

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.jorgecastilloprz.progressarc.ProgressArcView.show()' on a null object reference

john1jan avatar Aug 18 '16 08:08 john1jan

+1

daniellessa avatar Sep 12 '16 02:09 daniellessa

+1

zhenalexfan avatar Sep 29 '16 12:09 zhenalexfan

After checking the library source code, addArcView() is called by onMeasure() method. As a result of that, you should not use the show function until the activity is completely shown. A good place to call show() is in onWindowFocusChanged(hasFocus == true) function .

@Override public void onWindowFocusChanged(boolean hasFocus) {

if (hasFocus == true)
  fabProgressCircle.show();

}

You should not use the show() function in OnCreate, OnResume, OnStart.

helloray avatar Oct 11 '16 07:10 helloray

You can modify the source code . Adding an OnGlobalLayoutListener before invoking the method show() will work for me

public void show() {
        if (progressArc == null) {
            this.getViewTreeObserver().addOnGlobalLayoutListener(
                    new ViewTreeObserver.OnGlobalLayoutListener() {
                        @Override
                        public void onGlobalLayout() {
                            if (progressArc != null) {
                                progressArc.show();
                                getViewTreeObserver().removeOnGlobalLayoutListener(this);
                            }
                        }
                    });
        }else {
            progressArc.show();
        }
    }

wujushan avatar Nov 27 '17 08:11 wujushan