preferenceActivity方法的loadPreference()会在安装第一次应用程序时抛出异常

时间:2014-04-07 01:02:04

标签: java android sharedpreferences

我是android的新手,所以请耐心等待。 我正在努力学习偏好。我有一个mainActivity的应用,当按下的菜单图标显示MyPreferenceActivity时。它允许用户设置与主题相关的一些偏好。 在我的mainActivity中,我有一个loadPrefernces方法在安装应用时工作正常,但第一次引发错误:unable to start activity component info[..] java.lang.NullPointerException 这是我的代码: MainActivity

public class MainActivity extends Activity {

public static final String PREFERENCE_FILENAME = "com.id11298775.exercise6_preferences";
SharedPreferences mSharedPreferences;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    loadPreferences(); // this cause the error
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

    case R.id.action_settings:
        Intent i = new Intent(MainActivity.this, MyPreferenceActivity.class);
        startActivity(i);
        break;

    }

    return true;
}

@Override
protected void onResume() {
    super.onResume();
    loadPreferences();

}

private void loadPreferences() {

    mSharedPreferences = getSharedPreferences(PREFERENCE_FILENAME,
            MODE_PRIVATE);
        // Setting the textView related to the subject
        TextView subjectTv = (TextView) findViewById(R.id.activitymain_favouritesubjectresult_tv);
        subjectTv.setText(mSharedPreferences.getString("list_subject_pref",
                null));
        // Setting the TextView related to the URL
        TextView urlTv = (TextView) findViewById(R.id.activitymain_handbookurlresult_tv);
        urlTv.setText(mSharedPreferences.getString("et_subject_pref", null));
        // Setting the TextView related to the times selected
        TextView labTimeTv = (TextView) findViewById(R.id.activitymain_labtimeresult_tv);
        // @SuppressWarnings("unchecked")
        Map<String, ?> map = mSharedPreferences.getAll();
        Object cs = map.get("list_times_pref");
        labTimeTv.setText(cs.toString());
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我的MyPreferenceActivity

public class MyPreferenceActivity extends PreferenceActivity implements
    OnSharedPreferenceChangeListener {

@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    addPreferencesFromResource(R.xml.preference);

}

@SuppressWarnings("deprecation")
@Override
protected void onResume() {
    super.onResume();
    getPreferenceScreen().getSharedPreferences()
            .registerOnSharedPreferenceChangeListener(this);
}

@SuppressWarnings("deprecation")
@Override
protected void onPause() {
    super.onPause();
    getPreferenceScreen().getSharedPreferences()
            .unregisterOnSharedPreferenceChangeListener(this);
}

@SuppressWarnings("deprecation")
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
        String key) {

    if (key.equals("list_subject_pref")) {
        ListPreference preference = (ListPreference) findPreference(key);
        CharSequence currText = preference.getEntry();
        preference.setSummary(currText);
    } else if (key.equals("et_subject_pref")) {
        EditTextPreference preference = (EditTextPreference) findPreference(key);
        String newUrl = preference.getText().toString();
        preference.setSummary(newUrl);
    } else if (key.equals("list_times_pref")) {
        Set<String> selections = sharedPreferences.getStringSet(
                "list_times_pref", null);
        String[] selected = selections.toArray(new String[] {});
        String listSelection = "";

        for (int i = 0; i < selected.length; i++) {
            listSelection += selected[i] + " ";

        }
        MultiSelectListPreference multi = (MultiSelectListPreference) findPreference(key);
        multi.setSummary(listSelection);
    } else if (key.equals("ringtonePref")) {

    RingtonePreference preference = (RingtonePreference) findPreference("ringtonePref");

        String strRingtonePreference = ((SharedPreferences) preference).getString("ringtonePref", "none");
        Uri ringtoneUri = Uri.parse(strRingtonePreference);
        Ringtone ringtone = RingtoneManager.getRingtone(this, ringtoneUri);
        String name = ringtone.getTitle(this);
        preference.setSummary("select " + name);
    }

}

}

这里是logcat: enter image description here

我认为第一次安装应用程序时没有设置首选项,因此错误,但我无法找到防止异常的好方法。 有人可以帮我吗?

0 个答案:

没有答案