1

Here is a snippet of my code:

public class ChooseNumWorkoutsDialog extends DialogFragment implements OnClickListener {
    Button btnClose, btnFinished;
    NumberPicker np;

    public ChooseNumWorkoutsDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_numpicker, container);
        getDialog().setTitle("Number of Exercises");
        btnClose = (Button) findViewById(R.id.btnClose);
        btnFinished = (Button) findViewById(R.id.btnFinished);
        np = (NumberPicker) findViewById(R.id.np);
        //np.setMaxValue(20);
        //np.setMinValue(1);
        //np.setWrapSelectorWheel(false);
        //btnClose.setOnClickListener(this);
        //btnFinished.setOnClickListener(this);   
        return view;
    }

The XML file does contain all referenced buttons and numberPickers. When this is run, a Null Pointer exception is found at "np.setMaxValue(20);", the only way I can get it to work is if I comment out all of the commented out parts you see.

  • Is there some rule that I don't know of that states I can't set up my onclick listeners etc within a dialogfragment?
  • What is the best way to solve this problem?

1 Answer 1

4

Initialize your views in onActivityCreated(). From documentation:

Called when the fragment's activity has been created and this fragment's view hierarchy instantiated. It can be used to do final initialization once these pieces are in place, such as retrieving views or restoring state. It is also useful for fragments that use setRetainInstance(boolean) to retain their instance, as this callback tells the fragment when it is fully associated with the new activity instance. This is called after onCreateView(LayoutInflater, ViewGroup, Bundle) and before onStart().

Than you can call getView().findViewById(R.id.np);

or use np = (NumberPicker) view.findViewById(R.id.np); in onCreateView(), notice the "view."

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.