findViewById in DialogFragment - NullPointerException

Found the solution, instead of trying to find the view in public void onActivityCreated(Bundle savedInstanceState)

In public Dialog onCreateDialog(Bundle savedInstanceState)

I changed from

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
    LayoutInflater inflater = getActivity().getLayoutInflater();

    builder.setView(inflater.inflate(R.layout.activity_schedule_select_dialog, null))
        .setPositiveButton(R.string.ssd_select_positive, new DialogInterface.OnClickListener()
        {               
            public void onClick(DialogInterface dialog, int which)
            {
                mListener.onDialogPositiveClick(DFrag.this);                        
            }
        })
        .setNegativeButton(R.string.select_negative, new DialogInterface.OnClickListener()
        {               
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog().cancel();                        
            }
        });

To

AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.activity_schedule_select_dialog, null);

    builder.setView(view)
        .setPositiveButton(R.string.ssd_select_positive, new DialogInterface.OnClickListener()
        {               
            public void onClick(DialogInterface dialog, int which)
            {
                mListener.onDialogPositiveClick(DFrag.this);                        
            }
        })
        .setNegativeButton(R.string.select_negative, new DialogInterface.OnClickListener()
        {               
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.onDialogNegativeClick(DFrag.this);
                DFrag.this.getDialog().cancel();                        
            }
        });

And at the end added

Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);
spinner.setSelection(getArguments().getInt("SelectWeek"));

First of extract the Spinner object to a member of the class.

public class DFrag extends DialogFragment
{
    private Spinner mSpinner;
    ...

Then assign your spinner from the onCreateDialog() function

public Dialog onCreateDialog(Bundle savedInstanceState) 
{
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());       
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.activity_schedule_select_dialog, null);
    // Assign spinner
    mSpinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);
    builder.setView(view);
    // Set positive and negative buttons here
    ...
}

Now place the value of your spinner on the onCreateView() function

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
{
    mSpinner.setSelection(getArguments().getInt("SelectWeek"));
    ...
}

Cheers!

Tags:

Java

Android