marshmalloaw警告对话框一如既往的错误

时间:2016-10-06 10:03:04

标签: android android-activity

每个使用棉花糖的人都可能遇到警告对话框创建错误。 所以构建警报对话框的正常过程不起作用,它说(“你需要使用一个主题.AppCompat主题(或后代)与此活动。”)

我尝试在清单中为应用程序和活动更改主题,但不起作用。有人帮忙吗?

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
//        setSupportActionBar(toolbar);
//
//        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
//        fab.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
//                        .setAction("Action", null).show();
//            }
//        });


Button button;
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {

                        AlertDialog.Builder alert= new AlertDialog.Builder(getApplicationContext());

                            alert.setCancelable(false);
                        alert.setMessage("Are you sure that you want to close this app?");
                        alert.setPositiveButton("True", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                }
                        );
                        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alertManager= alert.create();
                        alertManager.setTitle("Warning!!!");
                        alertManager.show();
                    }
                }
        );
    }

1 个答案:

答案 0 :(得分:0)

点击按钮点击更新:

AlertDialog.Builder alert= new AlertDialog.Builder(<YourActivityName>this); // pass activity context reference

                            alert.setCancelable(false);

                        alert.setPositiveButton("True", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        finish();
                                    }
                                }
                        );
                        alert.setNegativeButton("No", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                dialog.cancel();
                            }
                        });
                        AlertDialog alertManager= alert.create();
                        alertManager.setTitle("Warning!!!");
                        alertManager.setMessage("Are you sure that you want to close this app?");  // Add message in alert Dialog 
                        alertManager.show();