Higure
Higure

Reputation: 235

NullPointerException when inflating a numberpicker xml in AlertDialog

I have an AlertDialog. In the onCreate I'm trying to inflate the xml layout but when I try to open the Dialog the activity crashes. I spent quite some time trying to figure out why but, honestly, I've no idea.

This is the logcat:

05-26 11:53:32.927: E/AndroidRuntime(2430): FATAL EXCEPTION: main
05-26 11:53:32.927: E/AndroidRuntime(2430): Process: it.sii.mywaiter, PID: 2430
05-26 11:53:32.927: E/AndroidRuntime(2430): java.lang.NullPointerException
05-26 11:53:32.927: E/AndroidRuntime(2430):     at it.sii.mywaiter.PiattoActivity$MenuQuantityDialogFragment.onCreateDialog(PiattoActivity.java:77)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.DialogFragment.getLayoutInflater(DialogFragment.java:398)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.BackStackRecord.run(BackStackRecord.java:684)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.os.Handler.handleCallback(Handler.java:733)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.os.Handler.dispatchMessage(Handler.java:95)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.os.Looper.loop(Looper.java:136)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at java.lang.reflect.Method.invokeNative(Native Method)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at java.lang.reflect.Method.invoke(Method.java:515)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-26 11:53:32.927: E/AndroidRuntime(2430):     at dalvik.system.NativeStart.main(Native Method)

This is the DialogFragment:

public class MenuQuantityDialogFragment extends DialogFragment {
    //crea il dialogfragment

    private NumberPicker np = null;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        setRetainInstance(true);            
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        LayoutInflater inflater = getActivity().getLayoutInflater();
        builder.setView(inflater.inflate(R.layout.number_picker, null));
        np = (NumberPicker) findViewById(R.id.numberPicker1);
        np.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS);
        np.setMinValue(1);
        np.setMaxValue(10);
        np.setWrapSelectorWheel(false);
        np.setValue(2);

        builder.setMessage(R.string.piatto_dialog_message)
               .setTitle(R.string.piatto_dialog_title)
               .setPositiveButton(R.string.piatto_dialog_ok, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       porzioni = np.getValue();
                       b_quantity.setText("Porzioni: "+ porzioni);
                       dismiss();
                   }
               })
               .setNegativeButton(R.string.piatto_dialog_annulla, new DialogInterface.OnClickListener() {
                   public void onClick(DialogInterface dialog, int id) {
                       dismiss();
                   }
               });

        return builder.create();
    }
}

And this is the xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <NumberPicker
        android:id="@+id/numberPicker1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

Upvotes: 0

Views: 234

Answers (2)

M D
M D

Reputation: 47817

You need to reference a NumberPicker by particular inflated view.

View view=inflater.inflate(R.layout.number_picker,null);
builder.setView(view);
np = (NumberPicker) view.findViewById(R.id.numberPicker1);

Upvotes: 2

Blackbelt
Blackbelt

Reputation: 157447

use the object returned by the inflater to look for the NumberPicker

View view = inflater.inflate(R.layout.number_picker, null);
builder.setView(view);
np = (NumberPicker) view.findViewById(R.id.numberPicker1);

It does not belongs the the Activity's view hierarchy (where you are looking for it)

Upvotes: 3

Related Questions