0

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

我应该怎么做才能使该对话框起作用?

 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();

            }
        });
    }

}

提前致谢...

4

3 回答 3

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();
于 2013-03-12T12:54:15.853 回答
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();

    }
};
于 2013-03-12T12:57:07.327 回答
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); 它奏效了

于 2013-03-12T12:57:55.300 回答