Weird NullpointerException when removing fabOption
See original GitHub issueI am doing this…
private void removeFabOption(FabOption fabOption) { expandableFabLayout.getPortraitConfiguration().getFabOptions().remove(fabOption); }
and get this exception…
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.pezcraft.myapplication, PID: 30800 java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getVisibility()' on a null object reference at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3416) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:3415) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1944) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1633) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7943) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1092) at android.view.Choreographer.doCallbacks(Choreographer.java:893) at android.view.Choreographer.doFrame(Choreographer.java:812) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1078) at android.os.Handler.handleCallback(Handler.java:907) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:216) at android.app.ActivityThread.main(ActivityThread.java:7625) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987) I/Process: Sending signal. PID: 30800 SIG: 9
But what’s weird is that it worked at first but when I started it a 2nd, 3rd, … time, I got this exception.
By the way, I am calling the function inside onBillingSetupFinished Listener. It worked perfectly outside the listener. `
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
expandableFabLayout = findViewById(R.id.expandableFabLayout);
prefManager = new PrefManager(getApplicationContext());
checkBox = findViewById(R.id.checkBox);
linearLayoutHighestHeartrate = findViewById(R.id.linearLayoutHighestHeartrate);
textViewHighestHeartrate = findViewById(R.id.textViewHighestHeartrate);
//if it's still the same day, do not reset the highest heartrate of the day
if(prefManager.getHighestHeartrateDate().equals(DateConverter.toDate(new Date()))) {
highestHeartrate = prefManager.getHighestHeartrate();
}
textViewHighestHeartrate.setText(getString(R.string.textViewHighestHeartrate, highestHeartrate));
//check subscription status
billingClient = BillingClient.newBuilder(this).enablePendingPurchases().setListener(this).build();
billingClient.startConnection(new BillingClientStateListener() {
@Override
public void onBillingSetupFinished(@NonNull BillingResult billingResult) {
if (billingResult.getResponseCode() == BillingResponseCode.OK){
billingClient.queryPurchasesAsync(SkuType.SUBS, (billingResult1, queryPurchasesSubs) -> {
if (queryPurchasesSubs.size() > 0){
handlePurchases(queryPurchasesSubs);
} else {
//if no item in purchase list means no, cancelled, or paused subscription
prefManager.setPremium(false);
removeFabOption(findViewById(R.id.fabOptionPremium));
}
//set keeprunning-checkbox
if(prefManager.isPremium() && prefManager.getKeepRunning()) {
checkBox.setChecked(true);
}
billingClient.queryPurchasesAsync(SkuType.INAPP, (billingResult2, queryPurchasesOnetime) -> {
if (queryPurchasesOnetime.size() > 0){
handlePurchases(queryPurchasesOnetime);
} else {
//if no item in purchase list means no, cancelled, or paused subscription
prefManager.setPremium(false);
removeFabOption(findViewById(R.id.fabOptionPremium));
}
//set keeprunning-checkbox
if(prefManager.isPremium() && prefManager.getKeepRunning()) {
checkBox.setChecked(true);
}
});
});
//query prices for BottomSheetDialog
getSubPrice();
getOnetimePrice();
}
}
@Override
public void onBillingServiceDisconnected() {
Log.d(DEBUG, "Billing Service disconnected.");
}
});`
Any ideas, how I can use it inside the listener?
Issue Analytics
- State:
- Created 2 years ago
- Comments:14 (8 by maintainers)
@kabumere Yes, it did. Thank you very much 😃
Hey @Pezcraft,
Did the above solution answer your question?