Android Alert Dialog崩溃了我的应用程序

时间:2013-03-12 12:48:23

标签: java android eclipse

每当我调试应用程序时,当我点击弹出对话框的按钮时,它就会崩溃。

如何使该对话框有效?

 public class Actionbar_BtnHandler extends Activity {
    Context context;

    public  Actionbar_BtnHandler (Context context)
    {

        this.context=context;
    }
    public void btn_handler (Button btn_mic,Button btn_post)
    {
        btn_mic.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context,"MICROPHONE",Toast.LENGTH_LONG).show();
            }
        });

        btn_post.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                 AlertDialog.Builder builder = new AlertDialog.Builder(context);
                    // Get the layout inflater
                    LayoutInflater inflater = ((Activity) context).getLayoutInflater();

                    // Inflate and set the layout for the dialog
                    // Pass null as the parent view because its going in the dialog layout
                    builder.setView(inflater.inflate(R.layout.dialog_signin, null))
                    // Add action buttons
                           .setPositiveButton("Post", new DialogInterface.OnClickListener() {
                               @Override
                               public void onClick(DialogInterface dialog, int id) {
                                   // sign in the user ...
                               }
                           })
                           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                               public void onClick(DialogInterface dialog, int id) {

                               }
                           });      
                     builder.create();

            }
        });
    }

}

提前致谢...

3 个答案:

答案 0 :(得分:3)

LayoutInflater inflater = ((Activity) context).getLayoutInflater();

不准确......演员似乎失败

尝试:

@Override
public void onClick(View v) {
    AlertDialog.Builder builder = new AlertDialog.Builder(context);
    // Get the layout inflater
    LayoutInflater inflater = Actionbar_BtnHandler.this.getLayoutInflater();

答案 1 :(得分:0)

尝试使用Handler编写AlertDialog代码..

 btn_post.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
       Handler handler = new Handler();
   handler.post(MessagealertDisplay);
    }
});
}





Runnable MessagealertDisplay = new Runnable() { 

    @Override
    public void run() {

         AlertDialog.Builder builder = new AlertDialog.Builder(context);
            // Get the layout inflater
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();

            // Inflate and set the layout for the dialog
            // Pass null as the parent view because its going in the dialog layout
            builder.setView(inflater.inflate(R.layout.dialog_signin, null))
            // Add action buttons
                   .setPositiveButton("Post", new DialogInterface.OnClickListener() {
                       @Override
                       public void onClick(DialogInterface dialog, int id) {
                           // sign in the user ...
                       }
                   })
                   .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                       public void onClick(DialogInterface dialog, int id) {

                       }
                   });      
             builder.create();

    }
};

答案 2 :(得分:0)

我用过 Actionbar_BtnHandler btns = new Actionbar_BtnHandle(Class.this);           btns.btn_handler(btn_post,btn_mic); 代替 Actionbar_BtnHandler btns = new Actionbar_BtnHandler(getApplicationContext);           btns.btn_handler(btn_post,btn_mic); 它工作了