Skip to content Skip to sidebar Skip to footer

Adding Assigned Values In Spinner Nullpointerexception

Like in my previous post: Adding Integers from Different Fragments in a ViewPager But now, I'm trying to assign a value in the spinner of FragA and add it to the int of FragB. Like

Solution 1:

Put this line in onCreateView

spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1); 

Solution 2:

-> spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);

replace that with

Spinnerspinner1= (Spinner) getView().findViewById(R.id.spinner1);

Solution 3:

Try this..

Use that in onCreateView

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            Viewview= inflater.inflate(R.layout.fraga, container, false); 
            spinner1 = (Spinner) view.findViewById(R.id.spinner1);
            ArrayAdapter<CharSequence>  adapter_a = ArrayAdapter.createFromResource(getActivity(), R.array.spinner1,android.R.layout.simple_spinner_item );
            spinner1.setAdapter(adapter_a);
            return view;
        }

Example :

publicclassFragAextendsFragment{

        Spinner spinner1;

        @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
           Viewview= inflater.inflate(R.layout.fraga, container, false); 
            spinner1 = (Spinner) view.findViewById(R.id.spinner1);
            ArrayAdapter<CharSequence>  adapter_a = ArrayAdapter.createFromResource(getActivity(), R.array.spinner1,android.R.layout.simple_spinner_item );
            spinner1.setAdapter(adapter_a);
            return view;
        }

        publicintgetInt() {

            inta=0;

            Strings1= spinner1.getSelectedItem().toString();

            if(s1.equals("Hello")) {
                a = 5;
            }
            elseif(s1.equals("Hi")) {
                a = 10; 
            }   
            return a;       
        }

        publicvoidonActivityCreated(Bundle savedInstanceState) {
            super.onActivityCreated(savedInstanceState);

        }

    }

Solution 4:

As onActivityCreated method is always called before the onCreateView and as you are trying to initialize your Spinner its not getting view.

Initialize your Spinner in onCreateView

EDITED:

Try to get your selected item by implementing the setonItemClickListener for your Spinner as below:

String s1;
    @OverridepublicViewonCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        spinner1 = (Spinner) getActivity().findViewById(R.id.spinner1);

        spinner1 .setOnItemSelectedListener(newOnItemSelectedListener() {

        @OverridepublicvoidonItemSelected(AdapterView<?> arg0, View arg1,
                int arg2, long arg3) {
             s1 = spinner1.getSelectedItem().toString();
                             getInt(s1);
        }

        @OverridepublicvoidonNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });
        return inflater.inflate(R.layout.fraga, container, false); 

    }

Also change yout getInt() method as below by passing the parameter of the value which is selected.

publicintgetInt(String s1) {

        int a = 0;
        if(s1.equals("Hello")) {
            a = 5;
        }
        elseif(s1.equals("Hi")) {
            a = 10; 
        }   
        return a;       
    }

Post a Comment for "Adding Assigned Values In Spinner Nullpointerexception"