Android 对话框显示片刻后消失;无法输入详细信息

Android 对话框显示片刻后消失;无法输入详细信息,android,dialog,radio-button,Android,Dialog,Radio Button,我正在尝试创建一个对话框。在对话框显示后,我应该选择“打开方向”或“返回方向”。如果选择了“打开方向”,则应返回true;如果选择了“返回方向”,则应返回false。但我的对话框不等待用户输入。过了一会儿它就消失了 我正在尝试扫描条形码。扫描条形码后,我打开一个对话框来判断是真是假。我会将扫描结果(布尔值)发送到HTTP服务器。您的代码应该可以正常工作。。看起来您遇到了一些异常。您的应用程序启动了吗?是的,它启动了。您使用的是哪个cntext?我已经更新了帖子并添加了更多代码。我正在传递活动上下

我正在尝试创建一个对话框。在对话框显示后,我应该选择“打开方向”或“返回方向”。如果选择了“打开方向”,则应返回true;如果选择了“返回方向”,则应返回false。但我的对话框不等待用户输入。过了一会儿它就消失了


我正在尝试扫描条形码。扫描条形码后,我打开一个对话框来判断是真是假。我会将扫描结果(布尔值)发送到HTTP服务器。

您的代码应该可以正常工作。。看起来您遇到了一些异常。您的应用程序启动了吗?是的,它启动了。您使用的是哪个
cntext
?我已经更新了帖子并添加了更多代码。我正在传递活动上下文您是否可以更改
showDialog(cntxt)到<代码>显示对话框(DirectionActivity.this)
public class DirectionActivity extends AppCompatActivity implements View.OnClickListener{
    Button scanBtn;
    TextView messageText, messageFormat, messageText2, messageFormat2, HTTPResult;
    String Result = null;
    QRResultData datum;
    String myServer = "https://swulj.000webhostapp.com/bus_fetch.php";
    String BUS_NUMBER= "EXTRA_BUS_NUMBER";
    String Param = "PARAM";
    String Stop = "STOP";
    Context cntxt = this;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_direction);
        scanBtn = findViewById(R.id.scanBtn);
        messageText = findViewById(R.id.textContent);
        messageFormat = findViewById(R.id.textFormat);
        messageText2 = findViewById(R.id.textContent2);
        messageFormat2 = findViewById(R.id.textFormat2);
        HTTPResult = findViewById(R.id.HTTPResult);
        // adding listener to the button
        scanBtn.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        IntentIntegrator intentIntegrator = new IntentIntegrator(this);
        intentIntegrator.setPrompt("Scan a barcode or QR Code");
        intentIntegrator.setOrientationLocked(true);
        intentIntegrator.initiateScan();
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        IntentResult intentResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
        // if the intentResult is null then
        // toast a message as "cancelled"
        if (intentResult != null) {
            if (intentResult.getContents() == null) {
                Toast.makeText(getBaseContext(), "Cancelled", Toast.LENGTH_SHORT).show();
            } else {
                // if the intentResult is not null we'll set
                // the content and format of scan message
                Result = intentResult.getContents();
                datum = parseResult(Result, myServer);
                showDialog(cntxt);

                //messageText.setText(Result);
                //messageFormat.setText(intentResult.getFormatName());
                messageText.setText(datum.seatNumber);
                messageFormat.setText(datum.busNumber);
                messageText2.setText(datum.original);
                messageFormat2.setText(Result);
                //Toast.makeText(getApplicationContext(),"You download is resumed2",Toast.LENGTH_LONG).show();
                HTTPConnection1 conn = new HTTPConnection1();
                conn.execute(datum);
            }
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    public void showDialog(Context cntxt)
    {
        final CharSequence[] items = {"On-Direction", "Return-Direction"};

        AlertDialog.Builder builder = new AlertDialog.Builder(cntxt);
        builder.setTitle("Alert Dialog with ListView and Radio button");
        //builder.setIcon(R.drawable.icon);
        builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {
                Toast.makeText(getApplicationContext(), items[item], Toast.LENGTH_SHORT).show();
            }
        });

        builder.setPositiveButton("Yes",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(cntxt, "Success", Toast.LENGTH_SHORT).show();
                    }
                });
        builder.setNegativeButton("No",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        Toast.makeText(cntxt, "Fail", Toast.LENGTH_SHORT).show();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
    public QRResultData parseResult(String Result, String urlArg)
    {
        QRResultData data = new QRResultData();
        /*StringTokenizer multiTokenizer = new StringTokenizer(Result, ";");

        if(multiTokenizer.hasMoreTokens())
        {
            data.busNumber = multiTokenizer.nextToken();
        }

        if(multiTokenizer.hasMoreTokens())
        {
            data.seatNumber = multiTokenizer.nextToken();
        }*/
        data.url = urlArg;
        data.original = Result;
        String[] split = Result.split(";");
        data.busNumber = split[0];
        if (split.length > 1) {
            data.seatNumber = split[1];
        }
        return data;
    }
.....
.....