user3247335
user3247335

Reputation: 163

Cannot set EditText? Issue From dialog in a fragment - Strange - Android Java

I've come across an issue which I've never had before. I have a Fragment containing a button. This button (ViewBookingButton) shows a popup dialog. When the dialog opens I would like to set a string in an EditText (AllBooking). Unfortunately it crashes and shows NullPointerExecption. What is going wrong? Here is my code:

ViewBookingButton.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v)
    {

        AlertDialog.Builder ViewBookingHelpBuilder = new AlertDialog.Builder(getActivity());
        ViewBookingHelpBuilder.setTitle("view booking:");

        LayoutInflater inflater = getActivity().getLayoutInflater();
        View DialogLayout = inflater.inflate(R.layout.view_booking, null);
        ViewBookingHelpBuilder.setView(DialogLayout);

        TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);

        AllBooking.setText("Hello World");//WHEN I REMOVE THIS THE DIALOG WORKS


        ViewBookingHelpBuilder.setPositiveButton("Done", new DialogInterface.OnClickListener() {

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


        AlertDialog helpDialog = ViewBookingHelpBuilder.create();
        helpDialog.show();

            }

    });

Upvotes: 0

Views: 95

Answers (1)

Raghunandan
Raghunandan

Reputation: 133560

Change this

 TextView AllBooking = (TextView)view.findViewById(R.id.AllBooking);

to

 TextView AllBooking = (TextView)DialogLayout.findViewById(R.id.AllBooking);

TextView belongs to the inflated layout and `findViewById looks for the view in the current view hierarchy.

Upvotes: 2

Related Questions