Search code examples
androidandroid-alertdialogandroid-appcompatillegalstateexception

Getting exception when creating AlertDialog android


I am getting the error:

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.

And it appears when my ErrorDialogFragment is doing builder.create();

Here is my ErrorDialogFragment code:

public class ErrorDialogFragment extends DialogFragment {

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    LayoutInflater inflater = getActivity().getLayoutInflater();

    String title = getArguments().getString("title");
    String message = getArguments().getString("message");


    View dialogView = inflater.inflate(R.layout.dialog_error, null);
    TextView tvTitle = (TextView) dialogView.findViewById(R.id.tvTitle);
    TextView tvMessage = (TextView) dialogView.findViewById(R.id.tvMessage);
    Button btnOk = (Button) dialogView.findViewById(R.id.btnOk);

    tvTitle.setText(title);
    tvMessage.setText(message);
    btnOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dismiss();
        }
    });

    builder.setView(dialogView);

    return builder.create();
}

}

The activity that calls the ErrorDialogFragment extends Activity and I need it fullscreen with the theme android:theme="@android:style/Theme.Holo.NoActionBar.Fullscreen". And need that theme else the UI Works in an odd way.

Any ideas?


Solution

  • Use android.app.DialogFragment instead of android.support.v7.app.AlertDialog.