0

I created a custom CheckBoxPreference class to prevent the CheckBoxPreference from being clicked when the user clicks on anything else than the CheckBox itself.

It worked perfectly yesterday before leaving but for a strange reason it doesn't work today. The app crashes and i get this error :

FATAL EXCEPTION: main Process: ca.qc.gouv.stat.kth, PID: 19543 java.lang.RuntimeException: Unable to start activity ComponentInfo{ca.qc.gouv.stat.kth/ca.qc.gouv.stat.kth.AppPreferenceActivity}: android.view.InflateException: Binary XML file line #24: Error inflating class ca.qc.gouv.stat.kth.CustomCheckBoxPreference

It crashes on this line in the code : addPreferencesFromResource(R.xml.preferences);

I'm almost sure i did not change anything to the project other than adding a Log.i line to my main activity this morning. I tried to comment this line. Clean the project cache. Refresh Gradle projects. A google search returned me many results on Error inflating class and i think there's as many solutions as there's questions asked. It looks like a very generic error.

If i replace my custom CheckBoxPreference in the preferences.xml file by a normal one it works. If i put breakpoints in the class of my custom CheckBoxPreference it doesn't break on them. Looks like the code of the class is never executed. I'm totally lost as i'm 100% positive it was working yesterday.

Here's the custom CheckBoxPreference class i did :

package ca.qc.gouv.stat.kth;

import android.content.Context;
import android.preference.CheckBoxPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.CheckBox;
import android.widget.TextView;

public class CustomCheckBoxPreference extends CheckBoxPreference {
    private CheckBoxPreference clickedCheckBoxPreference;

    CustomCheckBoxPreference(Context context) {
        super(context);
    }

    CustomCheckBoxPreference(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    CustomCheckBoxPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onBindView(View view) {
        super.onBindView(view);

        clickedCheckBoxPreference = this;

        CheckBox checkBox = (CheckBox) view.findViewById(android.R.id.checkbox);
        TextView title = (TextView) view.findViewById(android.R.id.title);

        title.setSingleLine(false);

        view.setOnClickListener(null);
        title.setOnClickListener(null);

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                CheckBox clickedCheckBox = (CheckBox) v;

                if (clickedCheckBox.isChecked()) {
                    clickedCheckBoxPreference.setChecked(true);
                } else {
                    clickedCheckBoxPreference.setChecked(false);
                }
            }
        });
    }
}

Here's the preferences.xml file :

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:sbp="http://schemas.android.com/apk/res/ca.qc.gouv.stat.kth">

    <EditTextPreference
            android:title="@string/url_title"
            android:key="url"
            android:defaultValue="@string/app_url"

    />
    <EditTextPreference
            android:title="@string/pwd_title"
            android:key="pwd"
            android:defaultValue="@string/app_pwd"

    />
    <ListPreference
            android:title="@string/orientation_title"
            android:key="orientation"
            android:entries="@array/orientation_display"
            android:entryValues="@array/orientation_values"
            android:defaultValue="@string/app_orientation"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
        android:title="@string/progress_title"
        android:key="progress"
        android:defaultValue="@string/app_progress"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/js_title"
            android:key="js"
            android:defaultValue="@string/app_js"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/tts_title"
            android:key="tts"
            android:defaultValue="@string/app_tts"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/clear_cache_title"
            android:key="clear_cache"
            android:defaultValue="@string/app_clear_cache"
            android:disableDependentsState="true"
    />
    <ListPreference
            android:title="@string/cache_mode_title"
            android:key="cache_mode"
            android:entries="@array/cache_mode_display"
            android:entryValues="@array/cache_mode_values"
            android:defaultValue="@string/app_cache_mode"
            android:dependency="clear_cache"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/clear_history_title"
            android:key="clear_history"
            android:defaultValue="@string/app_clear_history"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/clear_form_title"
            android:key="clear_form"
            android:defaultValue="@string/app_clear_form"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/keep_alive_title"
            android:key="keep_alive"
            android:defaultValue="@string/app_keep_alive"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/viewport_title"
            android:key="viewport"
            android:defaultValue="@string/app_viewport"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/fit_title"
            android:key="fit"
            android:defaultValue="@string/app_fit"
            android:dependency="viewport"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/zoom_support_title"
            android:key="zoom_support"
            android:defaultValue="@string/app_zoom_support"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/zoom_controls_title"
            android:key="zoom_controls"
            android:defaultValue="@string/app_zoom_controls"
            android:dependency="zoom_support"
    />
    <ca.qc.gouv.stat.kth.SeekBarPreference
            android:key="zoom_default"
            android:title="@string/zoom_default_title"
            android:defaultValue="@string/app_zoom_default"
            sbp:maxValue="100"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/error_back_btn_title"
            android:key="error_back_btn"
            android:defaultValue="@string/app_error_back_btn"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/error_home_btn_title"
            android:key="error_home_btn"
            android:defaultValue="@string/app_error_home_btn"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/restrict_host_title"
            android:key="restrict_host"
            android:defaultValue="@string/app_restrict_host"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/debug_info_title"
            android:key="debug_info"
            android:defaultValue="@string/app_debug_info"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/autocomplete_title"
            android:key="autocomplete"
            android:defaultValue="@string/app_autocomplete"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/adjust_height_title"
            android:key="adjust_height"
            android:defaultValue="@string/app_adjust_height"
    />
    <ca.qc.gouv.stat.kth.SeekBarPreference
            android:key="adjust_tolerance"
            android:title="@string/adjust_tolerance_title"
            android:defaultValue="@string/app_adjust_tolerance"
            android:dependency="adjust_height"
            sbp:maxValue="100"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/long_click_title"
            android:key="long_click"
            android:defaultValue="@string/app_long_click"
    />
    <ca.qc.gouv.stat.kth.SeekBarPreference
            android:key="settings_delay"
            android:title="@string/settings_delay_title"
            android:defaultValue="@string/app_settings_delay"
            sbp:maxValue="5"
    />
    <ca.qc.gouv.stat.kth.SeekBarPreference
            android:key="settings_nb_touch"
            android:title="@string/settings_nb_touch_title"
            android:defaultValue="@string/app_settings_nb_touch"
            sbp:maxValue="10"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/file_access_title"
            android:key="file_access"
            android:defaultValue="@string/app_file_access"
    />
    <ListPreference
            android:title="@string/js_file_access_title"
            android:key="js_file_access"
            android:entries="@array/js_file_access_display"
            android:entryValues="@array/js_file_access_values"
            android:defaultValue="@string/app_js_file_access"
    />
    <ca.qc.gouv.stat.kth.CustomCheckBoxPreference
            android:title="@string/content_url_access_title"
            android:key="content_url_access"
            android:defaultValue="@string/app_content_url_access"
    />
    <ListPreference
            android:title="@string/mixed_content_title"
            android:key="mixed_content"
            android:entries="@array/mixed_content_display"
            android:entryValues="@array/mixed_content_values"
            android:defaultValue="@string/app_mixed_content"
    />
    <Preference
            android:title="@string/exit_btn_lbl"
            android:key="exit"
    />
</PreferenceScreen>

Targeting api 26, min api 21;

FrancisC
  • 11
  • 3
  • Please read this before post a question :: https://stackoverflow.com/help/how-to-ask :: You should post your code here on SO not on an external site. – Barns Nov 07 '17 at 18:31

1 Answers1

1

Well after 3 hours of research i found the solution right after posting this question ...

It's because i did not declare the constructors as public. By default they are package protected and since the api calls them on addPreferencesFromResource it fails. It's strange cause i'm sure yesterday it was working and my constructors were not declared public. Anyway it works now and i know why so i'll move on.

FrancisC
  • 11
  • 3