Question

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.
}
Was it helpful?

Solution

In the code, you're using a Preference object, when in the XML you have a CheckBoxPreference. 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

CheckBoxPreference.OnPreferenceChangeListener() {
        public boolean onPreferenceChange(final Preference preference, final Object newValue) {
            // Your code here. Make good use of preference and newValue.
            // You can cast newValue to boolean, for example.
        }
}

Full working sample:

Preference settingWallpaperChangingIsActivated = (CheckBoxPreference) findPreference("checkbox_change_wallpaper_is_activated");

settingWallpaperChangingIsActivated.setOnPreferenceChangeListener(new CheckBoxPreference.OnPreferenceChangeListener() {
    public boolean onPreferenceChange(final Preference preference, final Object newValue) {


        boolean activated = (Boolean) newValue;
        updateStuff(activated);
        return true;


    }
});

OTHER TIPS

The error is on line 51 of hajj.auto.wallpaper.free.SettingsActivity in the onCreate() method. I can't tell you anymore than that without seeing that code. Although I guess it is probably because you try to add a listener to an object that hasn't been initialized yet (is probably still null).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top