为什么AlertDialog突然消失并在按下后退按钮后再次显示?

时间:2012-06-08 14:47:09

标签: android android-alertdialog

我有一个按钮,一次调用两个方法。那就是alarm()和sms()。 当我点击按钮时,AlertDialog显示但在我按下“STOP”按钮之前消失了。它消失后不久,我收到了短信。如果按下后退按钮,AlertDialog会再次出现。我好像遇到了什么事。

onCreate()方法:

btnAlarm = (Button) findViewById(R.id.btnAlarm);
        btnAlarm.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                alarm();
                sms();

            }
        });

alarm()方法:

public void alarm()
    {
        Alarm1 = MediaPlayer.create(MySetting.this, R.raw.lingcoll);

        //Turn on the alarm sound
        Alarm1.setLooping(true); // Set looping
        Alarm1.start();

        AlertDialog.Builder builder = new AlertDialog.Builder(MySetting.this);
        builder.setMessage(R.string.alert_stop)
            .setIcon(android.R.drawable.ic_alert)
            .setTitle(R.string.alert_dialog_warning)
            .setCancelable( false )
            .setPositiveButton(R.string.alert_btnStop, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int id) {


                    Alarm1.stop();
                    MySetting.isAlarm = false;

                }
            });

        AlertDialog alert = builder.create();
        alert.show();

        }

sms()方法:

public void sms() 
    {   

        TextView a = (TextView) findViewById(R.id.txtPhoneNo);

            String phoneNo = a.getText().toString();

            String message = "My Application works!";

                    PendingIntent pi = PendingIntent.getActivity(this, 0,
                new Intent(this, FinalSetting.class), 0);                
                SmsManager sms = SmsManager.getDefault();
                sms.sendTextMessage(phoneNo, null, message, pi, null);


    }

LogCat看起来很好,所以我不知道这个问题是什么。 有谁知道如何防止这??? 非常感谢您的进步..

1 个答案:

答案 0 :(得分:0)

听起来在某处有上下文切换。你可以在某处设置contextView或者崩溃。

我在想你只是在某处设置contextView,因为当你回击时,Dialog仍然在堆栈中存活。还要确保你解雇你的Dialog onclick。

最后,您可能需要检查您在对话框中使用的变量是否必须是最终的。 Eclipse通常会捕获这个,但它可能会导致崩溃。