在片段中访问对话框内的元素

时间:2013-02-01 10:46:13

标签: android nullpointerexception android-alertdialog

我想访问我在我的片段中放置对话框的spinner元素。 但是,只要单击按钮,应用程序就会崩溃并导致空指针异常。我想在按钮

的onclick监听器中访问对话框的视图
public class Plan extends Fragment {

Button add;
Button can;
Spinner spin;
View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.plan, container, false);

    add = (Button)view.findViewById(R.id.bAddPlan);

    //addItemsOnSpinner(view);

    add.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(getActivity());

            alertDialogBuilder.setTitle("Add Materials ");

            LayoutInflater inflater = getActivity().getLayoutInflater();

            alertDialogBuilder
                .setView(inflater.inflate(R.layout.dialog, null))
                .setPositiveButton("Add",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {

                    }
                  })

                .setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog,int id) {
                        // if this button is clicked, just close the dialog box and do nothing
                        dialog.cancel();
                    }
                });


                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();
                spin = (Spinner)alertDialog.findViewById(R.id.spinner1);

                alertDialog.show();


        }
    });


    return view;

}



@Override
public void onStart() {
    super.onStart();

}

}

0 个答案:

没有答案