1

任务:我试图让我的应用程序读取用户从首选项中选择的国家/地区,然后在应用程序再次使用 setCountry() 重新启动时使用与用户选择的国家/地区相对应的单选按钮上的 performClick() 方法。

问题:使用 performClick() 获取 NullPointerException。

日志猫:

E/AndroidRuntime(23373): 引起: java.lang.NullPointerException 01-04 15:20:27.025: E/AndroidRuntime(23373): 在geminide.software.mpg.Main.setcountry(Main.java:256) 01- 04


[setcountry() 方法](这就是问题发生的地方)

在首选项设置为的执行单击方法上捕获异常。如果首选项设置为 UK,然后重新打开应用程序,则会在 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. 
    }
};

[performClick() 应该在 onCreate 方法中调用的单选按钮]

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:");
        }
    });

[偏好类]

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;
        }
    });
}

}

[设置.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>

解决方案:已在全局和本地定义了单选按钮的变量。我删除了本地声明,异常消失了。

例外:

 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);

固定的:

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);
4

0 回答 0