0

I'm having trouble with number picker. When i try to add min and max values in java code for the numberpicker i get this error

Attempt to invoke virtual method 'void android.widget.NumberPicker.setMinValue(int)' on a null object reference

Obviously, it doesn't recognize my numberpicker, so that i can not add the values.

Somebody found a solution to this problem here, but it doesn't really fit me. Because in the next dialog i'll need another numberpicker and adjust it according to the previous one.

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState){
        AlertDialog.Builder bodyBuilder = new AlertDialog.Builder(getActivity());
        LayoutInflater inflator = getActivity().getLayoutInflater();
        bodyBuilder.setTitle(R.string.title);
        NumberPicker np = (NumberPicker) getActivity().findViewById(R.id.nambir);
        np.setMinValue(1);
        np.setMaxValue(100);
        final int npValue= np.getValue();
        bodyBuilder.setView(inflator.inflate(R.layout.layout_diyalog, null))
                .setPositiveButton("Continue", new DialogInterface.OnClickListener(){
                    @Override
                            public void onClick(DialogInterface dialog, int id){
                        Intent intent = new Intent(getActivity(), Olurmuki.class);
                        intent.putExtra("value", npValue);
                        startActivity(intent);
                    }
                })
                .setNegativeButton("Cancel",new DialogInterface.OnClickListener(){
                    @Override
                    public void onClick(DialogInterface dialog, int id){
                        MyDialog.this.getDialog().cancel();
                    }
                });
        return bodyBuilder.create();
}
Tahir Ferli
  • 636
  • 4
  • 16
  • 1
    Inflate the layout and use the inflated view to get your NumberPicker before setting the view. Example.. `View view = (do your inflate here)`. Then find NP by using view.findViewById(...). Then builder.setView(view). Apologies, typing from phone. – Zhi Kai Oct 07 '16 at 13:32
  • Yes, it worked @Kai , thanks. – Tahir Ferli Oct 07 '16 at 16:55
  • Yes it is the same issue @cricket_007 thanks as well. – Tahir Ferli Oct 07 '16 at 16:56

2 Answers2

0

Please note I'm a beginner and I'm not by Android Studio but hopefully this will work.

If I understand the question you want to access your number picker inside the dialog? If the number picker is inside the dialog then you need to access the builder view and then get the number picker.

This line is most likely the problem.

NumberPicker np = (NumberPicker) getActivity().findViewById(R.id.nambir);

Use this instead:

NumberPicker np = (NumberPicker) bodyBuilder.findViewById(R.id.nambir);

This will look for the Number picker inside the view of your Alert Dialog and you should get a reference.

Note if this doesn't work you can reference this and it might work for you! https://stackoverflow.com/a/13026250/4502550

Community
  • 1
  • 1
CardDeath
  • 132
  • 11
  • This would work until bodyBuilder has its view assigned. I've flagged this question as a duplicate, though, since it likely is the problem – OneCricketeer Oct 07 '16 at 13:45
0

try this

Create a getContext()

public Context getContext(){return this;}

and then

NumberPicker np = (NumberPicker) getContext().findViewById(R.id.nambir);

if it doens't work just create the number picker without the xml from the activity

public void Dialog(){


    final android.support.v7.app.AlertDialog.Builder alert = new android.support.v7.app.AlertDialog.Builder(this,R.style.CustomDialog);
    alert.setCancelable(true);


    LinearLayout l1 = new LinearLayout(getApplicationContext());


    l1.setOrientation(LinearLayout.HORIZONTAL);
    final NumberPicker number = new NumberPicker((this));
    number.setMaxValue(12);
    number.setMinValue(1);
    number.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
    number.setWrapSelectorWheel(true);
Diogo Rosa
  • 756
  • 1
  • 5
  • 24