0

Buenas como dice en el título, tengo el siguiente problema, tengo un alertdialog creado en una clase pero al intentar abrirlo la app me salta con el siguiente error:

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

El caso es que no entiendo el porque se está produciendo este error ya que en todas las activities y tanto en el manifest como en el styles.xml está puesto como tema por defecto Theme.AppCompat.NoActionBar

Aquí dejo parte de la clase:

private void showGroups(){

    ListAdapter adaptet = new ArrayAdapter(getApplicationContext(), R.layout.listview_groupsmain, R.id.groupName, g);
    listErrands.setAdapter(adaptet);
    listErrands.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {

            AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(getApplicationContext());
            alerDialogBuilder.setTitle(R.string.finisherrands);
            alerDialogBuilder.setMessage("Are you sure about that?");
            alerDialogBuilder.setCancelable(false);
            alerDialogBuilder.setPositiveButton(getString(R.string.yes), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finishErrands(ipServer+"finish.php", position);
                }
            });
            alerDialogBuilder.setNegativeButton(getString(R.string.no), new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    dialog.cancel();
                }
            });
            alerDialogBuilder.create().show();

        }
    });
}

Aquí el manifest:

 <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/Theme.AppCompat.NoActionBar">

y aquí el styles.xml:

<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/colorPrimary</item>
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
    <item name="colorAccent">@color/colorAccent</item>
</style>

He probado a cambiar el tema en la activity que me está dando el problema y tampoco a funcionado

1
  • Este error se debe a que estás pasando en el Builder del diálogo el context de la aplicación y la app no tiene ningún tema asociado. Para solucionarlo debes pasar el context de tu activity (this@YourActivity). el 22 abr. 2020 a las 15:13

1 respuesta 1

0

Al final conseguí solucionar el problema cambiando el getApplicationContext() por el nombredelaclase.this y ademas de esto cree en el archivo styles.xml un apartado para el tema del dialogo

<style name="MyAlertDialogStyle" parent="Theme.AppCompat.Light.Dialog.Alert">
    <!-- Used for the buttons -->
    <item name="colorAccent">#FFC107</item>
    <!-- Used for the title and text -->
    <item name="android:textColorPrimary">#FFFFFF</item>
    <!-- Used for the background -->
    <item name="android:background">#202732</item>
</style>troducir el código aquí

y el objeto del alertDialog quedaría asi

  AlertDialog.Builder alerDialogBuilder = new AlertDialog.Builder(Prueba.this, R.style.MyAlertDialogStyle);

Tu Respuesta

By clicking “Publica tu respuesta”, you agree to our terms of service and acknowledge you have read our privacy policy.

¿No es la respuesta que buscas? Examina otras preguntas con la etiqueta o formula tu propia pregunta.