Click here to Skip to main content
15,116,815 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.
Java
logAs = (Spinner) getActivity().findViewById(R.id.spinner5);
        List list = new ArrayList();
        list.add("Subscriber");
        list.add("Retailer");
        list.add("Administrator");
        ArrayAdapter dataAdapter = new ArrayAdapter(getActivity(), android.R.layout.simple_spinner_item, list);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        logAs.setAdapter(dataAdapter);
        logAs.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                       //chooseRetailerDetails();
                       // choodeRetailer.setVisibility(View.INVISIBLE);
                       
                        break;
                    case 1:
                        chooseRetailerDetails();
                        
                        //choodeRetailer.setVisibility(View.VISIBLE);
                        break;
                    case 3:
                        //choodeRetailer.setEnabled(false);
                        
                        break;
                }
            }
        });


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

-KR
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 ?

-KR
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(R.id.spinner6); 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:

Java
choodeRetailer = (Spinner)viewIinflated.findViewById(R.id.spinner6);
   

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