FABProgressCircle
FABProgressCircle copied to clipboard
Null Pointer Exception when calling show() function
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);
}
+1
In my case, addArcView()
is not being called.
+1, addArcView()
is not being called.
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.jorgecastilloprz.progressarc.ProgressArcView.show()' on a null object reference
+1
+1
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.
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();
}
}