0

我创建了一个自定义CheckBoxPreference类,以防止在CheckBoxPreference用户单击 CheckBox 本身以外的任何其他内容时单击 。

它昨天在离开之前运行良好,但由于一个奇怪的原因它今天不起作用。该应用程序崩溃,我收到此错误:

致命异常:主进程:ca.qc.gouv.stat.kth,PID:19543 java.lang.RuntimeException:无法启动活动 ComponentInfo{ca.qc.gouv.stat.kth/ca.qc.gouv.stat.kth .AppPreferenceActivity}:android.view.InflateException:二进制 XML 文件第 24 行:膨胀类 ca.qc.gouv.stat.kth.CustomCheckBoxPreference 时出错

它在代码中的这一行崩溃: addPreferencesFromResource(R.xml.preferences);

我几乎可以肯定,除了在今天早上的主要活动中添加 Log.i 行之外,我没有对项目进行任何更改。我试图评论这一行。清理项目缓存。刷新 Gradle 项目。谷歌搜索返回给我很多关于Error inflating 类的结果,我认为解决方案与提出的问题一样多。它看起来像一个非常普遍的错误。

如果我CheckBoxPreferencepreferences.xml文件中的自定义替换为普通文件,则它可以工作。如果我在我的自定义类中放置断点,CheckBoxPreference它不会中断它们。看起来类的代码永远不会执行。我完全迷失了,因为我 100% 肯定它昨天还在工作。

这是CheckBoxPreference我做的自定义类:

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);
                }
            }
        });
    }
}

这是preferences.xml文件:

<?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>

针对 api 26,最小 api 21;

4

1 回答 1

1

好吧,经过3个小时的研究,我在发布此问题后立即找到了解决方案...

这是因为我没有将构造函数声明为公共的。默认情况下,它们是包保护的,并且由于 api 调用它们addPreferencesFromResource失败。这很奇怪,因为我确定昨天它正在工作并且我的构造函数没有被宣布为公开。无论如何,它现在有效,我知道为什么,所以我会继续前进。

于 2017-11-07T19:00:36.477 回答