0

Android Studio 3.4

public class SubscribesFragment extends Fragment {

    @Override
    protected void initLogic() {
        IntentFilter intentFilterRefusePaperInvoice = new IntentFilter(ExistSubscribeWidget.ACTION_REFUSE_PAPER_INVOICE);
        LocalBroadcastManager.getInstance(getActivity()).registerReceiver(mMessageReceiver,
                intentFilterRefusePaperInvoice);

        super.initLogic();
    }

    private BroadcastReceiver mMessageReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, final Intent intent) {
            String action = intent.getAction();
            if (action.equals(ExistSubscribeWidget.ACTION_REFUSE_PAPER_INVOICE)) {
                String providerGUID = intent.getStringExtra(ProviderWidget.EXTRA_PROVIDER_TAG);
                String notificationSubscribeOwnerKey = intent.getStringExtra(ExistSubscribeWidget.NOTIFICATION_SUBSCRIBE_OWNERkEY);
                showConfirmDialogRefusePaperInvoice(providerGUID, notificationSubscribeOwnerKey);
            }
        }
    };

    private void showConfirmDialogRefusePaperInvoice(final String providerGUID, final String notificationSubscribeOwnerKey) {
        Activity actitivty = getActivity(); // NULL
        AlertDialog.Builder builder = new AlertDialog.Builder(actitivty); // NPE
        builder.setTitle(R.string.refuse_paper_dialog_title);
        View customView = AndroidUtil.getLinearLayout(actitivty, R.layout.refuse_paper_dialog);
builder.setView(customView);

}

But sometimes (not every time) in method showConfirmDialogRefusePaperInvoice the app crash with NPE:

 FATAL EXCEPTION: main
 Process: com.myproject.app, PID: 19430
 java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
    at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:222)
    at android.app.AlertDialog$Builder.<init>(AlertDialog.java:452)
    at com.myproject.app.fragment.SubscribesFragment.showConfirmDialogRefusePaperInvoice(SubscribesFragment.java:67)
    at com.myproject.app.fragment.SubscribesFragment.access$000(SubscribesFragment.java:38)
    at com.myproject.app.fragment.SubscribesFragment$1.onReceive(SubscribesFragment.java:60)
    at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:313)
    at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:121)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Alexei
  • 14,350
  • 37
  • 121
  • 240
  • 3
    It's because, may be your broadcast received in **detached state** of fragment, in such case activity reference becomes **null**. better is to check whether fragment is attached or not. – Jeel Vankhede May 27 '19 at 12:20
  • Can't you use the context from the `onReceive()` argument? But you have to make sure you're passing activity context when you're sending the broadcast. – Paresh P. May 27 '19 at 12:46
  • I think your question is a duplicate of this one : https://stackoverflow.com/questions/6215239/getactivity-returns-null-in-fragment-function – Vikas May 27 '19 at 12:53

2 Answers2

0

just put a context, this is a better approach inside fragment to getContext

  AlertDialog.Builder builder = new AlertDialog.Builder(mInflatedView.getContext);

where, mInflatedView = inflater.inflate(R.layout.fragment, container, false);
hemen
  • 1,460
  • 2
  • 16
  • 35
0

If you call showConfirmDialogRefusePaperInvoice before activity is created then yes your fragment will get null when calling getActivity(). You can make sure that activity is created in fragment by fragment callback onActivityCreated

Vishal Pawar
  • 4,324
  • 4
  • 28
  • 54