Hmm thnx..I must be doing something else wrong then..cause when I use this
code
private void loadPrefs() {

    ///SharedPreferences sp =
this.getActivity().getSharedPreferences("CHECKBOX", 0);

    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(
this);

boolean cbValue = sp.getBoolean("CHECKBOX", false);

String name = sp.getString("NAME", "");

if(cbValue){

cb.setChecked(true);

 }else{

cb.setChecked(false);

}

et.setText(name);

}

}


I get this error "The method getDefaultSharedPreferences(Context) in the
type PreferenceManager is not applicable for the arguments

 (TestFragment2)"


I thought had to replace this
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);

With this
SharedPreferences sp = this.getActivity().getSharedPreferences("CHECKBOX",
0);


and I get no errors, however this Throws a NullPointer Exception at
loadPrefs();


public final class TestFragment2 extends Fragment {
    TextView et;

    TextView text;

    SharedPreferences sp;

    View myView;

    CheckBox cb;


    @Override

    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        loadPrefs();
}


    @Override

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

        View myView = inflater.inflate(R.layout.fragment_2, container, false
);

        text = (TextView)myView.findViewById(R.id.textView2);

        et = (TextView)myView.findViewById(R.id.textView3);
        return myView;


}


       private void loadPrefs() {

       SharedPreferences sp = this.getActivity().getSharedPreferences(
"CHECKBOX", 0);

       boolean cbValue = sp.getBoolean("CHECKBOX", false);

       String name = sp.getString("NAME", "");

       if(cbValue){

       cb.setChecked(true);

       }else{

       cb.setChecked(false);

       }

       et.setText(name);

 }

}





On Thu, Feb 21, 2013 at 4:46 PM, Mark Murphy <[email protected]>wrote:

> Using the exact same code, if you wish.
>
> On Thu, Feb 21, 2013 at 7:40 PM, Marcelo Corvezola <[email protected]>
> wrote:
> > Normally I use something like the code below to load sharedPreference
> into
> > an activity..
> >
> > loadPrefs();
> >
> > }
> >
> >
> > private void loadPrefs() {
> >
> > SharedPreferences sp =
> PreferenceManager.getDefaultSharedPreferences(this);
> >
> > boolean cbValue = sp.getBoolean("CHECKBOX", false);
> >
> > String name = sp.getString("NAME", "YourName");
> >
> > if(cbValue){
> >
> > cb.setChecked(true);
> >
> > }else{
> >
> > cb.setChecked(false);
> >
> > }
> >
> > et.setText(name);
> >
> > }
> >
> >
> >
> > How are preferences loaded in a fragment Acivity? thnx
> >
> > --
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]
> > For more options, visit this group at
> > http://groups.google.com/group/android-developers?hl=en
> > ---
> > You received this message because you are subscribed to the Google Groups
> > "Android Developers" group.
> > To unsubscribe from this group and stop receiving emails from it, send an
> > email to [email protected].
> > For more options, visit https://groups.google.com/groups/opt_out.
> >
> >
>
>
>
> --
> Mark Murphy (a Commons Guy)
> http://commonsware.com | http://github.com/commonsguy
> http://commonsware.com/blog | http://twitter.com/commonsguy
>
> _The Busy Coder's Guide to Android Development_ Version 4.6 Available!
>
> --
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to [email protected]
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Android Developers" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
>

-- 
-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
--- 
You received this message because you are subscribed to the Google Groups 
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/groups/opt_out.


Reply via email to