質問

I got a dialog created in a DialogFragment. This works fine.

For my dialog I use a customized layout. This contains two buttons.

release_scan_dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

        <Button
            android:id="@+id/code"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scan 1" />

        <Button
            android:id="@+id/storageplace"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Scan 2" />
</LinearLayout>

ScanFragment.java

public class ScanFragment extends DialogFragment {

    public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) {
        AlertDialog dialog = new AlertDialog.Builder(this.getActivity())
            .setView(this.getActivity().getLayoutInflater().inflate(R.layout.release_scan_dialog, null))
            .setTitle(R.string.title_store)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

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

                }
            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

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

                }
            })
            .create();

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                }
            });

            // This will crush with a NullPointerException
            dialog.findViewById(R.id.code).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
        });

        return dialog;
    }
}

If I try to define the onClick for the both buttons a NullPointerException occurs. Which is the correct event to implement the onClick?

Thanks for any response.

役に立ちましたか?

解決

The code button can be found within the view inflated from the release_scan_dialog layout.

public class ScanFragment extends DialogFragment {

    public android.app.Dialog onCreateDialog(android.os.Bundle savedInstanceState) {

        // Inflate the view for the release_scan_dialog layout.
        final View release_scan_view = getActivity().getLayoutInflater().inflate(R.layout.release_scan_dialog, null);

        AlertDialog dialog = new AlertDialog.Builder(this.getActivity())
            .setView(release_scan_view)
            .setTitle(R.string.title_store)
            .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {

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

                }
            })
            .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {

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

                }
            })
            .create();

            dialog.setOnShowListener(new DialogInterface.OnShowListener() {

                @Override
                public void onShow(DialogInterface dialog) {
                    ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
                }
            });

            // Set the OnClickListener for the 'code' button in the release_scan_dialog layout.
            release_scan_view.findViewById(R.id.code).setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View view) {
                    ((AlertDialog) getDialog()).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
            }
        });

        return dialog;
    }
}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top