Dialog does not appear

后端 未结 5 2027
鱼传尺愫
鱼传尺愫 2021-01-12 01:19

I use following code:

public class Settings extends Activity implements OnClickListener {

    private Activity activity;
    private AlertDialog.Builder bui         


        
相关标签:
5条回答
  • 2021-01-12 02:03

    I had a somehow similar problem, in my case the dialog would appear and disappear in milliseconds: To solve it, don't call finish() or finishActivity() in the same method that calls your dialog.

    createNewDialog(SOME_CONSTANT).show();
    finish();
    

    this destroys your dialog even before you access it.

    0 讨论(0)
  • 2021-01-12 02:07

    you need to call builder.show().

    0 讨论(0)
  • 2021-01-12 02:17

    I have this issue and maybe this answer can help someone.

    I was running the code to show the AlertDialog on a non-ui thread. After using:

    runOnUiThread(new Runnable()
        {
            @Override
            public void run()
            {
                ShowAlert();
            }
        });
    

    the AlertDialog worked.

    0 讨论(0)
  • 2021-01-12 02:21

    You have to call show() method instead of create().

    Note: create() method only creates instance of Dialog but it won't show its.

    One suggestion:

    You can create method that returns Dialog like this:

    public Dialog createNewDialog(int type) {
       AlertDialog dlg = null;
       switch (type) {
          case SOME_CONSTANT:
             dlg = new AlertDialog.Builder(ActivityName.this / this)
                .setTitle("Title")
                .setMessage("Message")
                .setPositiveButton("Yes", null)
                .create();
          break;
       }
    }
    

    Then you can call it as:

    createNewDialog(SOME_CONSTANT).show();
    

    and your Dialog will be shown.

    Especially in your case you can reach your goal with this snippet of code:

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.bAdd:
                createNewDialog(SOME_CONSTANT).show();
                break;
            }
        }
    

    Hope it helps.

    0 讨论(0)
  • 2021-01-12 02:21

    The reason it is not showing is that .show() was not called on the AlertDialog

    @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            switch (v.getId()) {
            case R.id.bAdd:
                AlertDialog dialog = builder.create();
                dialog.show();
                break;
            }
    
        }
    
    0 讨论(0)
提交回复
热议问题