You need to use a Theme.AppCompat theme (or descendant) with this activity

前端 未结 30 4738
感动是毒
感动是毒 2020-11-21 04:42

Android Studio 0.4.5

Android documentation for creating custom dialog boxes: http://developer.android.com/guide/topics/ui/dialogs.html

If you want a custom d

30条回答
  •  不要未来只要你来
    2020-11-21 04:53

    In my experiences the problem was the context where I showed my dialog. Inside a button click I instantiate an AlertDialog in this way:

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

    But the context was not correct and caused the error. I've changed it using the application context in this way:

    In declare section:

    Context mContext;
    

    in the onCreate method

    mContext = this;
    

    And finally in the code where I need the AlertDialog:

    start_stop = (Button) findViewById(R.id.start_stop);
    start_stop.setOnClickListener( new View.OnClickListener()
         {
                    @Override
                    public void onClick(View v)
                    {
                        if (!is_running)
                        {
                            builder = new AlertDialog.Builder(mContext);
                            builder.setMessage("MYTEXT")
                                    .setCancelable(false)
                                    .setPositiveButton("SI", new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                        Task_Started = false;
                                        startTask();
                                        }
                                    })
                                    .setNegativeButton("NO",
                                            new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog, int id) {
                                            dialog.cancel();
                                        }
                                    });
                            AlertDialog alert = builder.create();
                            alert.show();
                        }
                }
            }
    

    This is the solution for me.

提交回复
热议问题