masmic masmic - 18 days ago 10
Android Question

Show progressDialog on fragment BroadcastReceiver

I have a fragment with some fields that the user has to fill. Lets say for example the telephone number.
When the user enters the number, the fragment calls a method from MainActivity and there are some operations done in the mainActivity, let's suppose:

1- Verification

2- Validation

I use a BroadcarstReceiver to communicate back from the MainActivity to the Fragment. So when the verification is done, I send a flag to the fragment using the BroadcastReceiver to show to the user a progressDialog.

At this point, the progressDialog should show until another flag for validation is received.

The problem is that I'm getting some kind of error about RuntimeException and the app is crashing at the point where it should show the progressDialog.

public class ProfileFragment extends Fragment {

ProgressDialog progressDialog;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_profile, container, false);

...

progressDialog = new ProgressDialog(getContext());
progressDialog.setContentView(R.layout.custom_progressdialog);

...

return rootView;
}

protected BroadcastReceiver ActivityDataReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if(ACTION_INTENT.equals(intent.getAction())) {
progressDialog.show();
}
if(ACTION_INTENT2.equals(intent.getAction())) {
progressDialog.dismiss();
}
}
}


This is what the logcat shows:

FATAL EXCEPTION: main
Process: com.detgsm, PID: 21736
android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.PhoneWindow.requestFeature(PhoneWindow.java:398)
at com.android.internal.app.AlertController.installContent(AlertController.java:249)
at android.app.AlertDialog.onCreate(AlertDialog.java:423)
at android.app.ProgressDialog.onCreate(ProgressDialog.java:249)
at android.app.Dialog.dispatchOnCreate(Dialog.java:578)
at android.app.Dialog.show(Dialog.java:314)
at com.detgsm.ProfileFragment$7.onReceive(ProfileFragment.java:271)
at android.support.v4.content.LocalBroadcastManager.executePendingBroadcasts(LocalBroadcastManager.java:297)
at android.support.v4.content.LocalBroadcastManager.access$000(LocalBroadcastManager.java:46)
at android.support.v4.content.LocalBroadcastManager$1.handleMessage(LocalBroadcastManager.java:116)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7224)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

Answer

You can't set contentView before showing progress dialog. See this link

You can find more info here.