Task: I am trying to get my app to read the country the user has selected from the preferences and then use the performClick() method on the radio button corresponding to the country the user has selected when the app starts again using setCountry().
Problem: Getting NullPointerException with performClick().
LOG CAT:
E/AndroidRuntime(23373): Caused by: java.lang.NullPointerException 01-04 15:20:27.025: E/AndroidRuntime(23373): at geminide.software.mpg.Main.setcountry(Main.java:256) 01-04
[setcountry() method] (This is where the problem occurs)
Exception caught on the perform click method that the preference is set to. If the preference was set to UK and then the app is re-opened it would be caught on the radiouk.performclick().
public void setcountry() {
SharedPreferences pref = PreferenceManager
.getDefaultSharedPreferences(this);
String value = pref.getString("updates_country", "United States");
if (value.equals("Europe")) {
radioeu.performClick();
} else if (value.equals("United States")) {
radious.performClick();
} else if (value.equals("United Kingdom")) {
radiouk.performClick(); ... this is line 256, exception is caught because preferences is set to UK.
}
};
[Radio button that performClick() should be calling within the onCreate method]
radioeu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
distancelabel.setText("Number of Kilometers:");
gastanklabel.setText(" Number of Liters: ");
unitlabel.setText("L/100km:");
}
});
[Preferences Class]
public class Settings extends PreferenceActivity {
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
// get the specified preferences using the key declared in
// preferences.xml
final ListPreference dataPref = (ListPreference) findPreference("updates_country");
// get the description from the selected item
dataPref.setSummary(dataPref.getEntry());
dataPref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object o) {
// TODO Auto-generated method stub
preference.setSummary(o.toString());
return true;
}
});
}
}
[Settings.xml]
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory
android:key="first_category"
android:title="Preferences" >
<ListPreference
android:defaultValue="2"
android:entries="@array/country"
android:entryValues="@array/countryValues"
android:key="updates_country"
android:summary=""
android:title="Default Country" />
</PreferenceCategory>
</PreferenceScreen>
Solution: Had defined the variables for the radio buttons both globally and locally. I removed the local declarations and the exception was gone.
Exception:
RadioButton radioeu, radious, radiouk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setcountry();
RadioButton radioeu = (RadioButton) findViewById(R.id.euradio);
RadioButton radious = (RadioButton) findViewById(R.id.usradio);
RadioButton radiouk = (RadioButton) findViewById(R.id.ukradio);
Fixed:
RadioButton radioeu, radious, radiouk;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setcountry();
radioeu = (RadioButton) findViewById(R.id.euradio);
radious = (RadioButton) findViewById(R.id.usradio);
radiouk = (RadioButton) findViewById(R.id.ukradio);