1

我想访问我在我的片段中放置在对话框中的微调器元素。但是,只要单击按钮,应用程序就会崩溃并导致空指针异常。我想在按钮的 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();

}

}

4

0 回答 0