onclick按钮时不显示Alertdialog

时间:2013-04-13 07:22:58

标签: android multithreading alertdialog

我创建了Alert对话框,当我点击按钮时它没有显示。它崩溃的应用程序。我的代码如下。我的代码有什么问题?请帮助我!!

backBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        TestHopeQueue.this);

                // set title
                alertDialogBuilder.setTitle("Remove Download");

                // set dialog message
                alertDialogBuilder
                        .setMessage("Click yes to exit!")
                        .setCancelable(false)
                        .setPositiveButton("Yes",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {

                                        for (int k = 0; k < trlist.size(); k++) {
                                            if (trlist.get(k) != null) {
                                                View v13 = linearlayout
                                                        .getChildAt(k);
                                                TextProgressBar pr = (TextProgressBar) v13
                                                        .findViewById(R.id.progressBar);

                                                if (pr.getProgress() < 100) {

                                                    trlist.get(k).interrupt();
                                                    trlist.set(k, null);
                                                    ViewVideoDelete(k);
                                                    Button btnDownButton = (Button) v13
                                                            .findViewById(R.id.btnDownload);
                                                    btnDownButton
                                                            .setVisibility(View.VISIBLE);

                                                    pr.setProgress(0);
                                                    pr.invalidate();
                                                    pr.setText("" + 0 + "%");

                                                    Button btnDelete = (Button) v13
                                                            .findViewById(R.id.btnDelete);
                                                    btnDelete
                                                            .setVisibility(View.GONE);
                                                    downloadqueuelist.remove(0);
                                                    if (k % 2 == 0) {
                                                        v13.setBackgroundColor(Color
                                                                .parseColor("#f8f8ff"));
                                                        // convertView.setBackgroundResource(R.drawable.whitebackground);
                                                    } else {
                                                        v13.setBackgroundColor(Color
                                                                .parseColor("#DCDCDC"));
                                                        // convertView.setBackgroundResource(R.drawable.greybackground);
                                                    }

                                                }
                                            } else {
                                                TestHopeQueue.this.finish();
                                            }

                                        }

                                    }
                                })
                        .setNegativeButton("No",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // if this button is clicked, just close
                                        // the dialog box and do nothing
                                        dialog.cancel();
                                    }
                                });


                //alertDialog.show();

                for (int k = 0; k < trlist.size(); k++) {

                    if (trlist.get(k)!= null) {
                        Log.v("log_tag"," Alert Dialog ");

                        break;
                    } else {
                        TestHopeQueue.this.finish();
                    }

                }
                AlertDialog alertDialog = new AlertDialog.Builder(TestHopeQueue.this).create();
                //AlertDialog alertDialog = alertDialogBuilder.create();
                alertDialog.show();

            }
        });

我在下面得到错误::

04-13 12:38:58.112: E/WindowManager(576): Activity com.example.testhopequeue.TestHopeQueue has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40585268 that was originally added here
04-13 12:38:58.112: E/WindowManager(576): android.view.WindowLeaked: Activity com.example.testhopequeue.TestHopeQueue has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40585268 that was originally added here
04-13 12:38:58.112: E/WindowManager(576):   at android.view.ViewRoot.<init>(ViewRoot.java:258)
04-13 12:38:58.112: E/WindowManager(576):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-13 12:38:58.112: E/WindowManager(576):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-13 12:38:58.112: E/WindowManager(576):   at android.view.Window$LocalWindowManager.addView(Window.java:424)
04-13 12:38:58.112: E/WindowManager(576):   at android.app.Dialog.show(Dialog.java:241)
04-13 12:38:58.112: E/WindowManager(576):   at com.example.testhopequeue.TestHopeQueue$1.onClick(TestHopeQueue.java:180)
04-13 12:38:58.112: E/WindowManager(576):   at android.view.View.performClick(View.java:2485)
04-13 12:38:58.112: E/WindowManager(576):   at android.view.View$PerformClick.run(View.java:9080)
04-13 12:38:58.112: E/WindowManager(576):   at android.os.Handler.handleCallback(Handler.java:587)
04-13 12:38:58.112: E/WindowManager(576):   at android.os.Handler.dispatchMessage(Handler.java:92)
04-13 12:38:58.112: E/WindowManager(576):   at android.os.Looper.loop(Looper.java:130)
04-13 12:38:58.112: E/WindowManager(576):   at android.app.ActivityThread.main(ActivityThread.java:3683)
04-13 12:38:58.112: E/WindowManager(576):   at java.lang.reflect.Method.invokeNative(Native Method)
04-13 12:38:58.112: E/WindowManager(576):   at java.lang.reflect.Method.invoke(Method.java:507)
04-13 12:38:58.112: E/WindowManager(576):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-13 12:38:58.112: E/WindowManager(576):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-13 12:38:58.112: E/WindowManager(576):   at dalvik.system.NativeStart.main(Native Method)

4 个答案:

答案 0 :(得分:0)

我在您的代码中发现您在其他部分中通过TestHopeQueue.this.finish();完成了您的活动,然后尝试在已完成的活动中显示警告对话框。

因此,您收到此错误消息。

答案 1 :(得分:0)

您可以直接在点击按钮上调用此方法。

    backBtn.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
               AlertBoxShow();

                }

   //   AlertBoxShow() method definition here
    public void AlertBoxShow(){
     AlertDialog.Builder alt_bld = new AlertDialog.Builder(this);
     alt_bld.setMessage("Do you want to close this application ?")
     .setCancelable(false)
     .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
      finish();
      }
})
         .setNegativeButton("No", new DialogInterface.OnClickListener() {
       public void onClick(DialogInterface dialog, int id) {
  //  Action for 'NO' Button
      dialog.cancel();
       }
         });
          AlertDialog alert = alt_bld.create();
           // Title for AlertDialog
          alert.setTitle("AlertDialogExample");
         // Icon for AlertDialog
         alert.setIcon(R.drawable.ic_launcher);
          alert.show();
   }

}

答案 2 :(得分:0)

同时你正在执行按钮1的两个动作onclick,显示dailog 2)你正在完成你的活动的内部循环,在显示导致错误的对话之前完成你的活动。

答案 3 :(得分:0)

Try This Using This Code You Get Alert Dialog..
==================================main Activity.java   
 package com.example.sample;

    import android.os.Bundle;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.content.Context;
    import android.content.DialogInterface;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;

    public class MainActivity extends Activity {
        Button b1;
        final Context context = this;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            b1 = (Button) findViewById(R.id.b1);

            b1.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    ShowDialog();
                }

            });
        }
        public void ShowDialog()
        {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                    context);

            // set title
            alertDialogBuilder.setTitle("Your Title");

            // set dialog message
            alertDialogBuilder
                    .setMessage("Click yes to exit!")
                    .setCancelable(false)
                    .setPositiveButton("Yes",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, close
                                    // current activity
                                    MainActivity.this.finish();
                                }
                            })
                    .setNegativeButton("No",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int id) {
                                    // if this button is clicked, just close
                                    // the dialog box and do nothing
                                    dialog.cancel();
                                }
                            });

            // create alert dialog
            AlertDialog alertDialog = alertDialogBuilder.create();

            // show it
            alertDialog.show();

        }

    }