How can I fix this error: You need to use a Theme.AppCompat theme (or descendant) with this activity

后端 未结 6 1565
醉梦人生
醉梦人生 2021-02-07 09:36

I searched all internet web sites to fix this error, but I couldn\'t. I just want to create AlertDialog with two button Yes and No.

This is my code:

impo         


        
6条回答
  •  深忆病人
    2021-02-07 10:18

    I face such a case and I was managed to solve it. Here it is:

    • define your Alert Dialog in MainActivity class, example:

    public class MainActivity extends AppCompatActivity {
    
       AlertDialog.Builder alertDialog;

    • then initialize it on OnCreate() method just like below:

    alertDialog = new AlertDialog.Builder(this);

    • then you can do the rest of alert Dialog customization like icon, title, message anywhere you like. in my case i used it in onFinish() of count down timer as shown below:

    public void onFinish() {
    
                    alertDialog.setIcon(android.R.drawable.alert_light_frame);
                    alertDialog.setTitle("You are done");
                    alertDialog.setMessage("you got it");
                    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
    
                            runTheCode();
                        }
                    });
                    alertDialog.show();

提交回复
热议问题