Question

For a school project, I'm building an application using PreferenceScreens as a framework.
I've created 2 new classes to suit my needs: SeekBarDialogPreference (designed to handle volumes and brightness) and DigitalPreference (for everything else).

class SeekBarDialogPreference extends DialogPreference {

    static HashMap<String, Integer> maxValues = new HashMap<String, Integer>(6);
    AudioManager am = (AudioManager) APP.getContext().getSystemService(Context.AUDIO_SERVICE);
    View views = new View(APP.getContext());
    SeekBar sb;
    CheckBox cb;

    @Override
    protected void onBindDialogView(View view) {
        sb = (SeekBar) views.findViewById(R.id.seekbar);
        cb = (CheckBox) views.findViewById(R.id.unchanged_check);
        sb.setMax(maxValues.get(this.getKey()));

        super.onBindDialogView(view);
    }

    @Override
    protected void onDialogClosed(boolean positiveresult) {
        super.onDialogClosed(positiveresult);
        if (positiveresult) {
            Editor editor = getEditor();
            editor.putInt(this.getKey(), cb.isChecked() ? -1 : sb.getProgress());
            editor.commit();
        }
    }

    void show() {
        onClick();
    }
}

and the DigitalPreference

public class DigitalPreference extends ListPreference{

    void show(){
        onClick();
    }

Here's the unifying TopPage, which is the main page

public class TopPage extends PreferenceActivity {

    @Override
    public void onCreate(Bundle allthethings) {
        super.onCreate(allthethings);
        Map<String, ?> shareprefs = PreferenceManager.getDefaultSharedPreferences(this).getAll();
        addPreferencesFromResource(R.xml.prefs);

    }

    @Override
    public void onStart() {
        getPrefs();
    }

    private SeekBarDialogPreference makeSeekBar(Preference pref) {
    return (SeekBarDialogPreference) findPreference(pref.getKey());
    }

    private DigitalPreference makeDigital(Preference pref) {
        return (DigitalPreference) findPreference(pref.getKey());
    }

    private void setClickListener(Preference preference, final boolean isSeekBar) {
        preference.setOnPreferenceClickListener(new OnPreferenceClickListener() {

            public boolean onPreferenceClick(Preference pref) {
                if (isSeekBar) {
                    makeSeekBarDialogPreference(pref).show();
                } else {
                    makeDigitalPreference(pref).show();
                }
                return true;
            }
        });
    }

    public void getPrefs() {

        SeekBarDialogPreference Ringvolume_preference = (SeekBarDialogPreference) findPreference("Ringvolume_preference");
        setClickListener(Ringvolume_Preference, true);

        //Instantiated each Preference and attached a ClickListener to each
}

I have all of the preferences declared in res/xml/prefs.xml. Now whenever I run the app, I get an immediate force close. I suspect there's something wrong with my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="com.gobernador"
      android:versionCode="1"
      android:versionName="0.1">
    <uses-sdk android:minSdkVersion="5"
              android:targetSdkVersion="10"/>

    <application android:label="@string/app_name"
             android:name="APP">
        <activity android:name=".TopPage"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

I've referenced Android: launch a custom Preference from a PreferenceActivity, among other stackoverflow questions, but I haven't been able to work out the problem.

If someone could shepherd me in the right direction, you would be getting me out of a huge rut. Thanks!

EDIT: After swimming through the logcat, I found an exception thrown in my prefs.xml file.

<?xml version="1.0" encoding="UTF-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  android:title="Settings">

    <PreferenceScreen android:title="Volumes"
                      android:summary="Ring, Notification, System, etc.">

        <com.gobernador.SeekBarDialogPreference android:key="Ringvolume_preference"
                                        android:title="Ring Volume"
                                        android:summary=""
                                        android:dialogLayout="@layout/seekbar_layout"/>

        <com.gobernador.SeekBarDialogPreference android:key="Notifyvolume_preference"
                                        android:title="Notification Volume"
                                        android:summary=""
                                        android:dialogLayout="@layout/seekbar_layout"/>
... and so on

where it tries to access the SeekBarDialogPreference. Why isn't it working?

EDIT: The Exception being thrown is a NoSuchMethodException: SeekBarDialogPreference(Context,AttributeSet). I defined that method, what did I miss?

Was it helpful?

Solution

I found my solution. It was a java error. I failed to list my constructors for SeekBarDialogPreference and DigitalPreference as public, so the system could not call them.

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