Rogy
Rogy

Reputation: 189

findViewById in DialogFragment - NullPointerException

The dialog I show has two Spinners, and for one of those I'd like to set the selected item (item to select is passed from the main activity with setArguments()/getArguments())

The problem is I can't get the spinner, instead I get a NullPointerException at

Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);

The DialogFragment code:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;

public class DFrag extends DialogFragment 
{
    public Dialog onCreateDialog(Bundle savedInstanceState) 
    {
    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();                        
            }
        });

        builder.setInverseBackgroundForced(true);

        return builder.create();        
    }


    public void onActivityCreated(Bundle savedInstanceState)
    {       
        View view = getView();

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

        super.onActivityCreated(savedInstanceState);
    }

    public interface DFragListener
    {
        public void onDialogPositiveClick(DialogFragment dialog);
        public void onDialogNegativeClick(DialogFragment dialog);
    }

    DFragListener mListener;


    @Override
    public void onAttach(Activity activity)
    {
        super.onAttach(activity);
        try 
        {
            mListener = (DFragListener) activity;
        }
        catch (ClassCastException e)
        {
            throw new ClassCastException(activity.toString() + " Listener not implemented");
        }
    }
}

As requested, layout for the dialog (activity_schedule_select_dialog.xml):

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

    <TextView 
        android:id="@+id/ssd_classLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_class_label"
        android:layout_marginRight="4dip"
        android:layout_marginLeft="4dip"/>
    <Spinner 
        android:id="@+id/ssd_classSelectSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="4dip"
        android:entries="@array/urnikClasses"/>

    <TextView 
        android:id="@+id/ssd_weekLabel"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/select_week_label"
        android:layout_marginRight="4dip"
        android:layout_marginLeft="4dip"/>    
    <Spinner 
        android:id="@+id/ssd_weeksSelectSpinner"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/weeksArray"/>

    <CheckBox 
        android:id="@+id/ssd_DefaultChkbox"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/ssd_DefaultChkBoxTxt"/>


</LinearLayout>

Logcat:

05-12 13:16:16.288: E/AndroidRuntime(4310): FATAL EXCEPTION: main
05-12 13:16:16.288: E/AndroidRuntime(4310): java.lang.NullPointerException
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.rogy.scks.urnik.DFrag.onActivityCreated(DFrag.java:91)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:1468)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:931)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Handler.handleCallback(Handler.java:725)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.os.Looper.loop(Looper.java:137)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at android.app.ActivityThread.main(ActivityThread.java:5195)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at java.lang.reflect.Method.invoke(Method.java:511)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
05-12 13:16:16.288: E/AndroidRuntime(4310):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 9

Views: 7272

Answers (4)

Peter Chaula
Peter Chaula

Reputation: 3721

This can also be done elegantly using Kotlin:

lateinit val myView: View;

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
    return AlertDialog.Builder(activity!!)
         .setPositiveButton(R.string.close) { dialog, which -> }
         .setView(LayoutInflater.from(activity).inflate(R.layout.my_fragment, null, false).apply {
                [email protected] = findViewById(R.id.my_view)
         })
         .create()

}


override fun onActivityCreated(savedInstanceState: Bundle?) {
   super.onActivityCreated(savedInstanceState)
  //do something with myView
}

Upvotes: 0

noahutz
noahutz

Reputation: 1217

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!

Upvotes: 2

Rogy
Rogy

Reputation: 189

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"));

Upvotes: 8

Bilbo Beggins
Bilbo Beggins

Reputation: 176

It's caused by this:

public void onActivityCreated(Bundle savedInstanceState)
    {       
        View view = getView();

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

        super.onActivityCreated(savedInstanceState);
    }

It gives you a NullPointerException, because the spinner, it's not already selected and it's empty, when the activity starts... You need to put this, on a onItemSelected listener.

public class SpinnerActivity extends Activity implements OnItemSelectedListener {
    ...

    public void onItemSelected(AdapterView<?> parent, View view, 
            int pos, long id) {
        // An item was selected. You can retrieve the selected item using
        // parent.getItemAtPosition(pos)
    }

    public void onNothingSelected(AdapterView<?> parent) {
        // Another interface callback
    }
}

See this for more information: http://developer.android.com/guide/topics/ui/controls/spinner.html#SelectListener

EDIT: You also need to import the "R" java file.. I don't see it on the import lists:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Spinner;

EDIT 2: To add a listener to the spinner do this:

Spinner spinner = (Spinner) view.findViewById(R.id.ssd_weeksSelectSpinner);

spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> arg0, View v, int position, long id)
        {
            // int position is the element you pressed
        }
    });

Upvotes: 1

Related Questions