Can you tell me why this click listener crashes the Android device when the user enters the settings screen?
/*
* Create the preference from the xml file. This will be used in a click
* listener.
*/
Preference settingWallpaperChangingIsActivated = (Preference) findPreference("checkbox_change_wallpaper_is_activated");
settingWallpaperChangingIsActivated
.setOnPreferenceClickListener(new OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
return true;
}
});
Here is the settings.xml file related to this click listener.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/category_title_wallpaper">
<CheckBoxPreference android:key="checkbox_change_wallpaper_is_activated"
android:title="@string/item_title_change_wallpaper" android:summary="@string/item_summary_change_wallpaper"
android:defaultValue="false" />
<ListPreference android:title="@string/list_title_time_before_changing_wallpaper"
android:key="list_time_before_changing_wallpaper" android:summary="@string/list_summary_time_before_changing_wallpaper"
android:entries="@array/label_time_before_changing_wallpaper"
android:entryValues="@array/value_time_before_changing_wallpaper"
android:defaultValue="Default" />
<!-- -->
</PreferenceCategory>
</PreferenceScreen>
If the click listener is commented out than the settings screen can be displayed so it looks like there's something wrong with the xml file or the click listener.
If I can get it to work, then I will put the additional coding before the return statement.
Thanks.
Truly, Emad
Update:
This is the entire class that is now working thanks for everyone's help:
import hajj.auto.wallpaper.R;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceClickListener;
import android.widget.Toast;
public class SettingsActivity extends PreferenceActivity implements
OnSharedPreferenceChangeListener {
SharedPreferences pref;
/*
* This is called when the class is created. It displays a Settings screen
* from the settings.xml file.
*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/*
* Read the settings definition from XML and show them in the current
* activity (screen).
*/
addPreferencesFromResource(R.xml.settings);
/*
* This Preference Manager is required for the change listener to work.
*/
pref = PreferenceManager.getDefaultSharedPreferences(this);
/*
* This will allow changes in lists to be trapped.
*/
pref.registerOnSharedPreferenceChangeListener(this);
/*
* Create the preference from the xml file. This will be used in a click
* listener.
*/
CheckBoxPreference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_changing_is_activated");
//Preference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_change_wallpaper_is_activated");
settingWallpaperChangingIsActivated.setOnPreferenceChangeListener(new CheckBoxPreference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(final Preference preference, final Object newValue) {
Toast.makeText(getApplicationContext(), "Test.",
Toast.LENGTH_LONG).show();
boolean activated = (Boolean) newValue;
// updateStuff(activated);
return true;
}
});
} // End method onCreate.
@Override
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
String key) {
// TODO Auto-generated method stub
} // End method onSharedPreferenceChanged.
private void finishThisActivity() {
this.finish();
} // End method finishThisActivity.
}
In the code, you're using a
Preference
object, when in the XML you have aCheckBoxPreference
. Those are two different things, and you can't cast one to the other, if I recall correctly.Also, the appropriate listener that I think you are wanting is
Full working sample: