JorgeCastilloPrz / FABProgressCircle

Material progress circle around any FloatingActionButton. 100% Guidelines.
1.25k stars 181 forks source link

Null Pointer Exception when calling show() function #29

Open Foivos-Stamopoulos opened 8 years ago

Foivos-Stamopoulos commented 8 years ago

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);
}
thiagokimo commented 8 years ago

+1

thiagokimo commented 8 years ago

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

mstedler commented 8 years ago

+1, addArcView() is not being called.

john1jan commented 8 years ago

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

daniellessa commented 8 years ago

+1

zhenalexfan commented 8 years ago

+1

helloray commented 8 years ago

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.

wujushan commented 6 years ago

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