簡體   English   中英

Android Alert對話框使我的應用程序崩潰

[英]Android Alert Dialog crashes my application

每當我調試應用程序時,當我單擊彈出對話框的按鈕時,它就會崩潰。

我應該怎么做才能使該對話框起作用?

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

            }
        });
    }

}

提前致謝...

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

嘗試使用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();

    }
};

我使用了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); 而且有效

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM