0

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

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

4 回答 4

0

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

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

于 2013-04-13T07:32:35.533 回答
0

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

于 2013-04-13T07:39:07.807 回答
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();
   }

}

于 2013-04-13T07:36:41.410 回答
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();

        }

    }
于 2013-04-13T07:42:40.667 回答