在Fragment中,getActivity()有时返回null

时间:2019-05-27 12:16:30

标签: android

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

}

但是有时(并非每次)在方法showConfirmDialogRefusePaperInvoice中,应用会因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)

2 个答案:

答案 0 :(得分:0)

仅放置一个上下文,这是在片段内部获取getContext的更好方法

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

where, mInflatedView = inflater.inflate(R.layout.fragment, container, false);

答案 1 :(得分:0)

如果在创建活动之前调用showConfirmDialogRefusePaperInvoice,则是的,在调用getActivity()时片段将为空。您可以确保通过片段回调onActivityCreated

在片段中创建活动。