0

I am developing an android app and I noticed I use the same dialogs in different fragments/Activities.

I was trying to wrap it in a class but my app is crashing whenever is time to show the dialog. Any ideas?

dialog class

public class Diaglogs {
+
+    private Context context;
+
+    public Diaglogs(Context context) {
+        this.context = context;
+    }
+
+    public void createPlaylistDialog() {
+        AlertDialog.Builder builder = new AlertDialog.Builder(context);
+        builder.setTitle("Enter name of the playlist");
+
+        // Set up the input
+        final EditText input = new EditText(context);
+
+        // Specify the type of input expected; this, for example, sets the input as a password, and will mask the text
+        input.setInputType(InputType.TYPE_CLASS_TEXT);
+        builder.setView(input);
+
+        // Set up the buttons
+        builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                String playListName = input.getText().toString();
+                if (!"".equals(playListName)) {
+                    Toast.makeText(context, "the c=playlist would be created here", Toast.LENGTH_SHORT);
+                } else {
+
+                }
+            }
+        });
+        builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
+            @Override
+            public void onClick(DialogInterface dialog, int which) {
+                dialog.cancel();
+            }
+        });
+        builder.show();
+    }
+
+}

error

java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96
Beto
  • 806
  • 3
  • 12
  • 33

2 Answers2

0

DialogFragment is intended for this purpose.

For simple "ok, no" you may use builder, but with DialogFragment it's easier to handle configuration changes.

PS: for errors always show your stacktace.

Maxim G
  • 1,479
  • 1
  • 15
  • 23
0

This type of error occurring when you can use one dialog in different type of Activity.

So, You can add specific dialog Theme to your dialog as below example:

 new AlertDialog.Builder(
  new ContextThemeWrapper(context, android.R.style.Theme_Dialog));
Mayur Sakhiya
  • 326
  • 2
  • 14
  • I am actually trying to make one class to hold all my dialogs. Meaning if I have a yes/no dialog call myClass.yesNoDialog() or myClass.inputDialog() So in the activity class I would instantiate MyClass. is there a way to do this? – Beto May 13 '16 at 04:02