///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)"
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;
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);
}
}
Using the exact same code, if you wish.
--
On Thu, Feb 21, 2013 at 7:40 PM, Marcelo Corvezola <mcorvezola@gmail.com> 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 android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> 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 android-developers+unsubscribe@googlegroups.com.
> 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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers+unsubscribe@googlegroups.com.
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 android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
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 android-developers+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
No comments:
Post a Comment