我在哪里定义自定义对话框中的onclick for按钮?

时间:2014-04-24 07:42:38

标签: android android-fragments nullpointerexception onclicklistener android-dialogfragment

我在DialogFragment创建了一个对话框。这很好。

对于我的对话框,我使用自定义布局。这包含两个按钮。

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;
    }
}

如果我尝试为两个按钮定义onClick,则会出现NullPointerException。哪个是实施onClick的正确事件?

感谢您的回复。

1 个答案:

答案 0 :(得分:2)

code按钮可以在release_scan_dialog布局充气的视图中找到。

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;
    }
}