在if else语句中未执行警报对话框代码

时间:2019-05-16 23:04:24

标签: android android-fragments android-alertdialog android-button

我有一个按钮,如果用户按下该按钮,它将向您选择的电话号码发送一条短信,效果很好。问题是,当用户尚未选择电话号码时,该应用程序未执行我的“警报”对话框代码,它将在其中显示一个消息框,显示“未选择调制解调器”,而只是跳转到else语句中的代码,尝试执行该操作。向不存在的号码发送消息。

btn_start.setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v)
    {
        // Build an Alert Dialog
        AlertDialog.Builder builder = new AlertDialog.Builder(v.getContext());

        // Set a title for alert dialog
        builder.setTitle("Start Engine?");

        // Ask the final question
        builder.setMessage("Are you sure want to start the Engine?");

        // Set the alert dialog yes button click listener
        builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                if(SelectedPhNo == null)
                {
                    // This code below is not being executed 
                    AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create();
                    alertDialog.setTitle("Modem Error");
                    alertDialog.setMessage("No Modem Selected");
                    alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
                            new DialogInterface.OnClickListener()
                            {
                                public void onClick(DialogInterface dialog, int which)
                                {
                                    dialog.dismiss();
                                }
                            });
                    alertDialog.show();
                }
                else
                {
                    // Start Engine
                    Intent intent = new Intent(getContext(), CommandsFragment.class);
                    PendingIntent pi = PendingIntent.getActivity(getContext(), 0, intent, 0);

                    SmsManager sms = SmsManager.getDefault();
                    ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.SEND_SMS}, 1);
                    sms.sendTextMessage(SelectedPhNo, null, start, pi, null);

                    Toast.makeText(getContext(), "Start Engine Command Sent Successfully", Toast.LENGTH_LONG).show();

                    inboxArrayAdapter.insert("Command: " + start + "\n" + strDate, 0);

                }
            }
        });

        // Set the alert dialog no button click listener
        builder.setNegativeButton("No", new DialogInterface.OnClickListener()
        {
            @Override
            public void onClick(DialogInterface dialog, int which)
            {
                // Do something when No button clicked
                Toast.makeText(getContext(), "Command Aborted",Toast.LENGTH_SHORT).show();
            }
        });

        AlertDialog dialog = builder.create();

        // Display the alert dialog on interface
        dialog.show();
    }
});

0 个答案:

没有答案