I have this onClickListener in onCreate method of my PreferenceActivity, but it gives me error.
Here is the PrefereceActivity:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings);
Button button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something.
}
});
}
it gives me this error:
unable to start activity componentinfo java.lang.nullpointerexception
any idea what am I doing wrong?
EDIT: My SettingsPreference opens Dialog that holds that "button2".
The Button
is causing your NullPointerException
because you haven't set a layout and therefore it is null
. You shouldn't need to use buttons in a PreferenceActivity
anyway.
There's a perfectly good example of using PreferenceActivity over at the Android developer site: http://developer.android.com/reference/android/preference/PreferenceActivity.html