简体   繁体   English

与往常一样,marshmalloaw警报对话框错误

[英]marshmalloaw alert dialog error as always

Everyone who use marshmallow may have faced alert dialog creation error. 使用棉花糖的每个人都可能遇到警告对话框创建错误。 So the normal process to build alert dialog is not working, it says ("You need to use a Theme. AppCompat theme (or descendant) with this activity.") 因此,构建警报对话框的正常过程无法正常进行,它说(“此活动需要使用一个主题。AppCompat主题(或后代)。”)

I have tried changing themes from manifest for both application and activity but does not work. 我已经尝试从清单中更改应用程序和活动的主题,但是不起作用。 Any one there to help? 有谁可以帮忙吗?

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();
                    }
                }
        );
    }

Updated your button click like this: 像这样更新了您的按钮单击:

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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM