javaandroidxmldialogpreferencescreen

Adding a Calendar to PreferenceScreen


I'm using DatePreference.java class to use as a preference in my xml file. The problem is that i get an error: Binary XML file line #496: Error inflating class com.fsck.k9.ui.settings.account.DatePreference note that line 496 is the first line of the xml code i posted.

If not suggesting another way to add a calendar to a PreferenceScreen would help too. Thanks

package com.fsck.k9.ui.settings.account;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.DatePicker;

public class DatePreference extends DialogPreference {
    private int lastDate = 0;
    private int lastMonth = 0;
    private int lastYear = 0;
    private String dateval;
    private CharSequence mSummary;
    private DatePicker picker = null;
    public static int getYear(String dateval) {
        String[] pieces = dateval.split("-");
        return (Integer.parseInt(pieces[0]));
    }

    public static int getMonth(String dateval) {
        String[] pieces = dateval.split("-");
        return (Integer.parseInt(pieces[1]));
    }

    public static int getDate(String dateval) {
        String[] pieces = dateval.split("-");
        return (Integer.parseInt(pieces[2]));
    }

    public DatePreference(Context ctxt, AttributeSet attrs) {
        super(ctxt, attrs);

        setPositiveButtonText("Set");
        setNegativeButtonText("Cancel");
    }

    @Override
    protected View onCreateDialogView() {
        picker = new DatePicker(getContext());

        // setCalendarViewShown(false) attribute is only available from API level 11
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            picker.setCalendarViewShown(false);
        }

        return (picker);
    }

    @Override
    protected void onBindDialogView(View v) {
        super.onBindDialogView(v);

        picker.updateDate(lastYear, lastMonth + 1, lastDate);
    }

    @Override
    protected void onDialogClosed(boolean positiveResult) {
        super.onDialogClosed(positiveResult);

        if (positiveResult) {
            lastYear = picker.getYear();
            lastMonth = picker.getMonth();
            lastDate = picker.getDayOfMonth();

            String dateval = String.valueOf(lastYear) + "-"
                    + String.valueOf(lastMonth) + "-"
                    + String.valueOf(lastDate);

            if (callChangeListener(dateval)) {
                persistString(dateval);
            }
        }
    }

    @Override
    protected Object onGetDefaultValue(TypedArray a, int index) {
        return (a.getString(index));
    }

    @Override
    protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
        dateval = null;

        if (restoreValue) {
            if (defaultValue == null) {
                Calendar cal = Calendar.getInstance();
                SimpleDateFormat format1 = new SimpleDateFormat("yyyy-MM-dd");
                String formatted = format1.format(cal.getTime());
                dateval = getPersistedString(formatted);
            } else {
                dateval = getPersistedString(defaultValue.toString());
            }
        } else {
            dateval = defaultValue.toString();
        }
        lastYear = getYear(dateval);
        lastMonth = getMonth(dateval);
        lastDate = getDate(dateval);
    }

    public void setText(String text) {
        final boolean wasBlocking = shouldDisableDependents();

        dateval = text;

        persistString(text);

        final boolean isBlocking = shouldDisableDependents();
        if (isBlocking != wasBlocking) {
            notifyDependencyChange(isBlocking);
        }
    }

    public String getText() {
        return dateval;
    }

    public CharSequence getSummary() {
        return mSummary;
    }

    public void setSummary(CharSequence summary) {
        if (summary == null && mSummary != null || summary != null
                && !summary.equals(mSummary)) {
            mSummary = summary;
            notifyChanged();
        }
    }
}

In the xml part where i refer to DatePreference

  <com.fsck.k9.ui.settings.account.DatePreference
            android:key="keyname"
            android:title="Enter start date"
            android:defaultValue="2014-08-01"
            android:summary="Summary"/>

Detailed error

android.view.InflateException: Binary XML file line #496: Error inflating class com.fsck.k9.ui.settings.account.DatePreference
            at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:258)
            at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:287)
            at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:354)
            at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:356)
            at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:162)
            at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:112)
            at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:137)
            at android.support.v7.preference.PreferenceManagerFix.inflateFromResource(PreferenceManagerFix.java:58)
            at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:448)
            at com.fsck.k9.ui.settings.account.AccountSettingsFragment.onCreatePreferencesFix(AccountSettingsFragment.kt:53)
            at com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:62)
            at android.support.v4.app.Fragment.performCreate(Fragment.java:2331)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386)
            at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188)
            at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071)
            at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115)
            at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380)
            at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245)
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703)
            at android.os.Handler.handleCallback(Handler.java:873)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:193)
            at android.app.ActivityThread.main(ActivityThread.java:6669)
            at java.lang.reflect.Method.invoke(Native Method)
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
         Caused by: java.lang.ClassCastException: com.fsck.k9.ui.settings.account.DatePreference cannot be cast to android.support.v7.preference.Preference
            at android.support.v7.preference.PreferenceInflater.createItem(PreferenceInflater.java:251)
            at android.support.v7.preference.PreferenceInflater.createItemFromTag(PreferenceInflater.java:287) 
            at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:354) 
            at android.support.v7.preference.PreferenceInflater.rInflate(PreferenceInflater.java:356) 
            at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:162) 
            at android.support.v7.preference.PreferenceInflater.inflate(PreferenceInflater.java:112) 
            at android.support.v7.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:137) 
            at android.support.v7.preference.PreferenceManagerFix.inflateFromResource(PreferenceManagerFix.java:58) 
            at android.support.v7.preference.PreferenceFragmentCompat.setPreferencesFromResource(PreferenceFragmentCompat.java:448) 
            at com.fsck.k9.ui.settings.account.AccountSettingsFragment.onCreatePreferencesFix(AccountSettingsFragment.kt:53) 
            at com.takisoft.fix.support.v7.preference.PreferenceFragmentCompat.onCreate(PreferenceFragmentCompat.java:62) 
            at android.support.v4.app.Fragment.performCreate(Fragment.java:2331) 
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1386) 
            at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1188) 
            at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:1071) 
            at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:115) 
            at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2380) 
            at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2338) 
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2245) 
            at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:703) 
            at android.os.Handler.handleCallback(Handler.java:873) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:193) 
            at android.app.ActivityThread.main(ActivityThread.java:6669) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858) 

Solution

  • You should change import android.preference.DialogPreference to import android.support.v7.preference.DialogPreference.

    That is because it looks like you are using the Support Library, be careful when doing so because you always need to use the classes starting with android.support.