views:

297

answers:

1

I keep getting nullpointer exception when i switch to my preferenceactivity. I have no idea what wrong but i keep getting a error when i load in my preference which is done programmitically.

public class SettingsFrontEnd extends PreferenceActivity implements
   OnSharedPreferenceChangeListener {
public static final String NO_SELECTION = "0&#8243";

public static final String PASSWORD_KEY = "PASSWORDKEY";
public static final String SELECTED_TARGET_KEY = "SelectedTargetKey";
public final String QUICK_CONTACT_KEY = "QUICK_CONTACT_KEY";

private android.content.SharedPreferences.Editor editor;
private HelpiDB mDbHelper;

private EditTextPreference passwordPreference;

private SharedPreferences pref;

private CheckBoxPreference quickcontactPreference;
private ListPreference targets;

@Override
public boolean onCreateOptionsMenu(Menu menu) {

MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main_menu, menu);

return true; }

@Override
public boolean onOptionsItemSelected(MenuItem item) {

switch (item.getItemId()) { case R.id.settings_menu:

 break;

case R.id.contact_menu: if (targets.getValue().equals(NO_SELECTION)) { Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show(); } else { Intent myIntent = new Intent(SettingsFrontEnd.this, ContactFrontEnd.class); startActivityForResult(myIntent, 0); } break; case R.id.map_menu: if (targets.getValue().equals(NO_SELECTION)) { Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show(); } else { Intent myIntent = new Intent(SettingsFrontEnd.this, ContactFrontEnd.class); startActivityForResult(myIntent, 0); } break; case R.id.insert_menu: if (targets.getValue().equals(NO_SELECTION)) { Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show(); } else { Intent myIntent = new Intent(SettingsFrontEnd.this, InsertFrontEnd.class); startActivityForResult(myIntent, 0); } break;

default: // put your code here } return false; }

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

if (key.equals(SELECTED_TARGET_KEY)) { editor = pref.edit(); editor.putString(key, targets.getValue()); } if (key.equals(PASSWORD_KEY)) { editor = pref.edit(); editor.putString(key, passwordPreference.getText()); } if (key.equals(QUICK_CONTACT_KEY)) { editor = pref.edit(); editor.putBoolean(QUICK_CONTACT_KEY, quickcontactPreference.isChecked()); }

}

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState); pref.registerOnSharedPreferenceChangeListener(this);

setPreferenceScreen(createPreferenceHierarchy()); }

private PreferenceScreen createPreferenceHierarchy() {

PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this); PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this); dialogBasedPrefCat.setTitle(R.string.title_preferences); root.addPreference(dialogBasedPrefCat); // builds list from DB mDbHelper = new HelpiDB(this); mDbHelper.open(); Cursor c = mDbHelper.getCheckedContacts(); startManagingCursor(c); int count = c.getCount(); CharSequence[] entries = new CharSequence[count]; CharSequence[] entryValues = new CharSequence[count]; c.moveToFirst(); for (int i = 0; i < count; i++) { entries[i] = c.getString(c.getColumnIndexOrThrow(HelpiDB.KEY_NAME)); entryValues[i] = c.getString(c.getColumnIndexOrThrow(HelpiDB.KEY_ID)); c.moveToNext(); } targets = new ListPreference(this); targets.setEntries(entries); targets.setEntryValues(entryValues); targets.setDefaultValue(NO_SELECTION); targets.setDialogTitle("Main Contact");

targets.setKey(SELECTED_TARGET_KEY); targets.setTitle("Main Contact"); targets.setSummary("First person contacted using HelpiCall"); dialogBasedPrefCat.addPreference(targets); // add other preference screens passwordPreference = new EditTextPreference(this); passwordPreference.setTitle("Password"); passwordPreference.setSummary("Password need for others to find you"); passwordPreference.setDialogTitle("Password"); passwordPreference.setKey(PASSWORD_KEY); root.addPreference(passwordPreference);

quickcontactPreference = new CheckBoxPreference(this); quickcontactPreference.setTitle("Quick Contact"); quickcontactPreference.setSummary("HelpiCall upon start up"); quickcontactPreference.setDefaultValue(false); quickcontactPreference.setKey(QUICK_CONTACT_KEY); root.addPreference(quickcontactPreference);

return root; }

}

+1  A: 
public class SettingsFrontEnd extends PreferenceActivity implements
      OnSharedPreferenceChangeListener {
public static final String NO_SELECTION = "0&#8243";

public static final String PASSWORD_KEY = "PASSWORDKEY";
public static final String SELECTED_TARGET_KEY = "SelectedTargetKey";
public final String QUICK_CONTACT_KEY = "QUICK_CONTACT_KEY";

private android.content.SharedPreferences.Editor editor;
private HelpiDB mDbHelper;

private EditTextPreference passwordPreference;

private SharedPreferences pref;

private CheckBoxPreference quickcontactPreference;
private ListPreference targets;

@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main_menu, menu);

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.settings_menu:

    break;
case R.id.contact_menu:
    if (targets.getValue().equals(NO_SELECTION)) {
 Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show();
    } else {
 Intent myIntent = new Intent(SettingsFrontEnd.this, ContactFrontEnd.class);
 startActivityForResult(myIntent, 0);
    }
    break;
case R.id.map_menu:
    if (targets.getValue().equals(NO_SELECTION)) {
 Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show();
    } else {
 Intent myIntent = new Intent(SettingsFrontEnd.this, ContactFrontEnd.class);
 startActivityForResult(myIntent, 0);
    }
    break;
case R.id.insert_menu:
    if (targets.getValue().equals(NO_SELECTION)) {
 Toast.makeText(this, "Choose a Main COntact", Toast.LENGTH_LONG).show();
    } else {
 Intent myIntent = new Intent(SettingsFrontEnd.this, InsertFrontEnd.class);
 startActivityForResult(myIntent, 0);
    }
    break;

default:
    // put your code here
}
return false;
}

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

if (key.equals(SELECTED_TARGET_KEY)) {
    editor = pref.edit();
    editor.putString(key, targets.getValue());
}
if (key.equals(PASSWORD_KEY)) {
    editor = pref.edit();
    editor.putString(key, passwordPreference.getText());
}
if (key.equals(QUICK_CONTACT_KEY)) {
    editor = pref.edit();
    editor.putBoolean(QUICK_CONTACT_KEY, quickcontactPreference.isChecked());
}

}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pref.registerOnSharedPreferenceChangeListener(this);

setPreferenceScreen(createPreferenceHierarchy());
}

private PreferenceScreen createPreferenceHierarchy() {
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
PreferenceCategory dialogBasedPrefCat = new PreferenceCategory(this);
dialogBasedPrefCat.setTitle(R.string.title_preferences);
root.addPreference(dialogBasedPrefCat);
// builds list from DB
mDbHelper = new HelpiDB(this);
mDbHelper.open();
Cursor c = mDbHelper.getCheckedContacts();
startManagingCursor(c);
int count = c.getCount();
CharSequence[] entries = new CharSequence[count];
CharSequence[] entryValues = new CharSequence[count];
c.moveToFirst();
for (int i = 0; i < count; i++) {
    entries[i] = c.getString(c.getColumnIndexOrThrow(HelpiDB.KEY_NAME));
    entryValues[i] = c.getString(c.getColumnIndexOrThrow(HelpiDB.KEY_ID));
    c.moveToNext();
}
targets = new ListPreference(this);
targets.setEntries(entries);
targets.setEntryValues(entryValues);
targets.setDefaultValue(NO_SELECTION);
targets.setDialogTitle("Main Contact");

targets.setKey(SELECTED_TARGET_KEY);
targets.setTitle("Main Contact");
targets.setSummary("First person contacted using HelpiCall");
dialogBasedPrefCat.addPreference(targets);
// add other preference screens
passwordPreference = new EditTextPreference(this);
passwordPreference.setTitle("Password");
passwordPreference.setSummary("Password need for others to find you");
passwordPreference.setDialogTitle("Password");
passwordPreference.setKey(PASSWORD_KEY);
root.addPreference(passwordPreference);

quickcontactPreference = new CheckBoxPreference(this);
quickcontactPreference.setTitle("Quick Contact");
quickcontactPreference.setSummary("HelpiCall upon start up");
quickcontactPreference.setDefaultValue(false);
quickcontactPreference.setKey(QUICK_CONTACT_KEY);
root.addPreference(quickcontactPreference);

return root;
}

}

related questions