Ich entwickle eine Android-App und habe festgestellt, dass ich dieselben Dialoge in verschiedenen Fragmenten/Aktivitäten verwende.Wie man Dialoge in der Klasse einpackt?
Ich habe versucht, es in eine Klasse zu wickeln, aber meine App stürzt ab, wann immer es Zeit ist, den Dialog zu zeigen. Irgendwelche Ideen?
Dialogklasse
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();
+ }
+
+}
Fehler
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
Könnten Sie auch Aktivitätscode laden? –
Ich denke, das Problem ist nicht zum Anzeigen des Alarmdialogs, überprüfen Sie diese anwser..http: //stackoverflow.com/a/21815015/1537419 – Srinivasan