0

以前の入力に基づいて、ユーザーに入力を求めようとしています。

いくつかのコンテキストを明確にして追加させてください。
このアプリケーションはサイコロを振るシミュレーターです。メイン アクティビティは、サイコロの数と各サイコロの面の数を表示します。これは、メニューからサブ アクティビティを開くことによって変更されます。このサブアクティビティは、 を使用して結果を求めて呼び出され、startActivityForResult()完全に 1RadioGroupつのボタンと 2 つのボタン (「同意する」と「キャンセル」) で構成され、String追加のアクティビティをメインアクティビティに返します (「XdN」の形式で - たとえば「1d20」)。 、メイン アクティビティはこれを解析して使用し、それに応じてサイコロの数と面の数を調整します。

このシステムは、「1d6」、「1d10」などの例では非常にうまく機能し、私の知る限り問題ではありません

さて、ここに私の問題
があります:「カスタム」を渡すラジオボタンがありますString(「XdN」のようなものではなくString)。選択すると (そして最後に [同意する] をクリックすると)、ダイアログが開き、ユーザーにカスタム カウント値を求めるプロンプトが表示されます。ダイアログが表示され、レイアウトは正しく、本来あるべきように見えますが、レイアウトは数秒後に消え、ユーザーが入力したはずの値ではなく、現在のカウント値を渡します。これにより、次のようなエラーが LogCat (私は Eclipse と ADT を使用しています) にもスローされます。

Application            Tag             Text
org.x3chaos.nicedice   WindowManager   Activity org.x3chaos.nicedice.DiceActivity
                                       has leaked window com.android.internal.pol
                                       icy.impl.PhoneWindow$DecorView@44786f20 th
                                       at was originally added here
org.x3chaos.nicedice   WindowManager   android.view.WindowLeaked: Activity org.x3
                                       chaos.nicedice.DiceActivity has leaked win
                                       dow com.android.internal.policy.impl.Phone
                                       Window$DecorView@44786f20 that was origina
                                       ly added here
org.x3chaos.nicedice   WindowManager   at android.view.ViewRoot.<init>(ViewRoot.j
                                       ava:247)

[more if needed]

--

Button button_acceptのメソッドは次のandroid:onClickとおりです。

public void accept(View view) {

    RadioGroup group = (RadioGroup) findViewById(R.id.dice_radiogrp);
    RadioButton selected = (RadioButton) findViewById(group
            .getCheckedRadioButtonId());
    String tmpResult = selected.getText().toString();

    if (!tmpResult.matches("[0-9|xX](.*)[Dd][0-9](.*)")) {

        // Show dialog

        LayoutInflater li = LayoutInflater.from(this);
        View dialogView = li.inflate(R.layout.prompt_dice, null);

        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setView(dialogView);
        builder.setTitle("Custom Dice");
        builder.setCancelable(false);

        final EditText editCount = (EditText) findViewById(R.id.edit_promptDiceCount);
        final EditText editSides = (EditText) findViewById(R.id.edit_promptDiceSides);

        builder.setPositiveButton("Accept", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                boolean flag = false;
                String textCount, textSides;
                textCount = editCount.getText().toString();
                if (textCount.equals("")) {
                    textCount = "1";
                    flag = true;
                }
                textSides = editSides.getText().toString();
                if (textSides.equals("")) {
                    textSides = "20";
                    flag = true;
                }

                int count = Integer.parseInt(textCount);
                int sides = Integer.parseInt(textSides);

                if (count > 20)
                    count = 20;
                if (sides > 20)
                    sides = 20;

                String finRes = count + "d" + sides;

                setResultExtra(finRes);

                if (flag) {
                    String msg = "Invalid input: set dice to " + finRes;
                    Toast toast = Toast.makeText(context, msg,
                            Toast.LENGTH_SHORT);
                    toast.show();
                }

            }

        });

        builder.setNegativeButton("Cancel", new OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.cancel();
                exitActivity(false);
            }

        });

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

    } else {
        result = tmpResult;
    }

    exitActivity(true);

}

他のすべての機能は機能するため、これが機能しない理由はわかりません。これは私の最初のアプリであり、これまで触れたことのないいくつかのオブジェクトとメソッドを使用しているため、私が台無しにしているものだと確信しています。:]

編集:この質問を読んだ後Intent、ダイアログが終了するのを待っていないのではないかと思い始めています。しかし、ダイアログを呼び出すアクティビティが結果に対して呼び出されるため、これはあまり意味がありません。

4

1 に答える 1

1

Android のダイアログは非同期です。これを表示して、コードがここで待機することを期待することはできません。そして、あなたがすることはdialog.show()、すぐにできるexitActivity()ので、あなたが作った方法でうまくいくようです。

編集

コードの簡単な修正は、おそらくこの部分を置き換えることです。

} else {
    result = tmpResult;
}

exitActivity(true);

} else {
    result = tmpResult;
    exitActivity(true);
}

これexitActivity()が問題の原因だからです。

于 2012-12-03T19:32:26.723 に答える