Click here to Skip to main content
15,112,662 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a fragment class with 2 spinners, one spinner has 3 values namely retailer, admin and subscriber, when the retailer option is clicked the 2nd spinner must be visible and when the other 2 options are clicked the 2nd spinner must be invisible.

the code i used is as follows
this is the first spinner in onResume method of the fragment.
logAs = (Spinner) getActivity().findViewById(;
        List list = new ArrayList();
        ArrayAdapter dataAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);
        logAs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                       // choodeRetailer.setVisibility(View.INVISIBLE);
                    case 1:
                    case 3:

now in the case statement when i try to set the visibility of the second spinner i get a null pointer exception.
Krunal Rohit 27-Oct-15 3:41am
Where did you get a null pointer exception ?

Rahul Ramakrishnan 27-Oct-15 4:35am
i get null pointer exception when i put use choodeRetailer.setVisibility(View.INVISIBLE);
chooseRetailer is the 2nd spinner
Krunal Rohit 27-Oct-15 4:38am
have you done that findViewById() for the spinner ?

Rahul Ramakrishnan 27-Oct-15 4:41am
yes is all declared. i am debugging the code line by line
Richard MacCutchan 27-Oct-15 4:47am
Where is choodeRetailer declared and set?
Rahul Ramakrishnan 27-Oct-15 4:51am
chooseRetailer = (Spinner) getActivity().findViewById(; was declared when json data is populated .

when i declared it in onResume method it work fine

1 solution

I think there may be a problem in your declaration of choodeRetailer spinner. If you are using fragments it should be like this:

choodeRetailer = (Spinner)viewIinflated.findViewById(;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900