Custom AlertDialog not showing

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-24 09:35:25

问题


I have a TextViewer Activity , that has a button, and when I click on it I want to popup an AlertDialog with list. I followed this link but it does not work (no popup). I believe the context is wrong. I used the following code:

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.resources);
    ImageButton btnlist = (ImageButton)findViewById(R.id.list);
    btnlist.setOnClickListener(new View.OnClickListener() {
             public void onClick (View v){                  

              if (Vars.bookchapter>1){
               final CharSequence[] items = {"Red", "Green", "Blue"};
               Context mContext = getBaseContext();
               AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
               builder.setTitle("Pick a color");
               builder.setItems(items, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int item) {
                       Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
                   }
               });
               AlertDialog alert = builder.create();
              }else{
               //Nothing
              }
             }});         
     }
        }

回答1:


You haven't called the show() method. Do this:

AlertDialog alert = builder.create();
alert.show();


来源:https://stackoverflow.com/questions/3160231/custom-alertdialog-not-showing

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!