我正在開發一個android應用程序,我注意到我在不同的片段/活動中使用相同的對話框。如何在課堂中包裝對話框?
我試圖把它包裝在一個類中,但是每當有時間顯示對話框時,我的應用程序都崩潰了。有任何想法嗎?
對話框類
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();
+ }
+
+}
錯誤
java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
你能上傳活動代碼也? –
我認爲問題不在於顯示警報對話框,請檢查這個anwser.http://stackoverflow.com/a/21815015/1537419 – Srinivasan