0

I have given this to save data with sharedpreference

so I want to get value to a spinner, here I have given this

SharedPreferences spf = getSharedPreferences("myprfs", Context.MODE_PRIVATE);
String ret = spf.getString("ctyp","");

Here am getting the value to a String, I want to assign a same value to a spinner.

for(int i=0;i<2;i++)                                       
    if(ret.equals(spinner.getItemAtPosition(i).toString())){  
    spinner.setSelection(i);                                  
     break;                                                
    }        

I am getting this error

'java.lang.Object android.widget.Spinner.getItemAtPosition(int)' on a null object reference  

Can any one suggest me correct ans..

ravi
  • 474
  • 1
  • 3
  • 11

1 Answers1

0

Your spinner should be null. Set a breakpoint to confirm that. Remember to run in debug mode to make the breakpoint to "break"

Most probably:

  1. not done a findViewById
  2. using ButterKnife and forgot to bind it

If you've done the above, but spinner is still null, in which lifecycle method are you running above code? Maybe you're trying to touch the UI before it's loaded

Diego Freniche
  • 5,225
  • 3
  • 32
  • 45