【问题标题】:Error inflating checkbox in PreferenceActivityPreferenceActivity 中的错误膨胀复选框
【发布时间】:2016-08-01 22:01:20
【问题描述】:

我的 pref_general.xml 文件有一些问题,因为我收到以下错误消息:

android.view.InflateException: Binary XML file line #19: Error inflating class android.widget.CheckBox

然后 logcat 在我的 PreferenceActivity 中指向我调用“addPreferencesFromResource(R.xml.pref_general);”的行

经过几个小时的尝试解决问题,我仍然不知道问题出在哪里,所以也许有人对此很熟悉,或者只是另一双眼睛可以发现问题。

pref_general.xml:

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

<PreferenceCategory
    android:title="General">
    <!--  <SwitchPreference
        android:key="NOTIF"
        android:title="Activer ou dأ©sactiver les notifications" /> -->
    <CheckBoxPreference
    android:defaultValue="true"
    android:key="NOTIF"
    android:title="Activer/Désactiver les notifications" />

    <Preference
        android:key="cache"
        android:title="Vider le cache"/>


    <Preference
        android:key="FAQ"
        android:title="FAQ"
        android:summary="Questions fréquentes"
        />
    <Preference
        android:key="feedback"
        android:title="FeedBack"
        android:summary="S'il vous plaît envoyer nous un FeedBack"
        />
    <Preference
        android:key="version"
        android:title="Version"
        android:summary="1.0.0(Build A750)"
        />
    <Preference
        android:key="Copyright"
        android:title="Copyright"
        android:summary="Toutes les matières contenues sur ce site sont Protégées par Nextice Inc, droit d'auteur et ne peuvent pas étre reproduits, distribués, transmis, affichés, publiأés ou diffusés sans l'premission écrit préalable de la société Nextice."
        android:selectable="true"
        android:enabled="false"
        />

    </PreferenceCategory>
 </PreferenceScreen>

另外,我在 Android 5.1 中尝试这个一切都很好,但是在 api 19(在我的手机中)没有任何工作?

设置活动:

public class SettingsActivity extends PreferenceActivity {

CheckBoxPreference notification;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.pref_general);


    LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
    Toolbar bar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings, root, false);
    root.addView(bar, 0); // insert at top
    bar.setNavigationOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(SettingsActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    });



    SharedPreferences settingsPrefs = PreferenceManager.getDefaultSharedPreferences(this);
    notification = (CheckBoxPreference) findPreference("NOTIF");
    notification.setOnPreferenceChangeListener(new OnPreferenceChangeListener(){

        public boolean onPreferenceChange(Preference preference,
                Object newValue) {
            if (newValue.toString().equals("true"))
            {
                notificationsOn();
                //PushService.setDefaultPushCallback(getApplicationContext(), MainActivity.class);
                //PushService.subscribe(getApplicationContext(), null, MainActivity.class, R.drawable.ic_notification);

            }
            else
            {
                notificationsOff();
                //PushService.setDefaultPushCallback(getApplicationContext(), null);
                //PushService.unsubscribe(getApplicationContext(), null);
                //PushService.unsubscribe(getApplicationContext(), "");
            }
            return true;
        }

        private void notificationsOn() {
            // TODO Auto-generated method stub
            /*Toast.makeText(SettingsActivity.this, "Notifications: Activée", Toast.LENGTH_SHORT).show();
            PushService.setDefaultPushCallback(getApplicationContext(), MainActivity.class);
            Pushbots.sharedInstance().setNotificationEnabled(true);*/

        }

        private void notificationsOff() {
            // TODO Auto-generated method stub
            /*Toast.makeText(SettingsActivity.this, "Notifications: Desactivée", Toast.LENGTH_SHORT).show();
            PushService.setDefaultPushCallback(getApplicationContext(), null);
            Pushbots.sharedInstance().setNotificationEnabled(false);*/

        }

    });



    Preference myPref = (Preference) findPreference("feedback");
    myPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

            Intent emailIntent = new Intent(Intent.ACTION_SENDTO, Uri.fromParts(
                    "mailto", "feedback@soft-grip-support.esy.es", null));
            emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Votre Sujet");
            emailIntent.putExtra(Intent.EXTRA_TEXT, "Votre FeedBack (Texte)");
            startActivity(Intent.createChooser(emailIntent, "Envoyer email à"));

            return false;

        }
    });

    Preference ver =  (Preference) findPreference("version");
    ver.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

         Toast.makeText(SettingsActivity.this, "1.0.0(Build A750)", Toast.LENGTH_LONG).show();

            return false;

        }
    });

    Preference cache =  (Preference) findPreference("cache");
    cache.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {


            deleteCache(SettingsActivity.this);
            Toast.makeText(SettingsActivity.this, "Cache Vidée", Toast.LENGTH_LONG).show();

            return false;

        }
    });

    Preference faq = (Preference)findPreference("FAQ");
    faq.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference arg0) {

            String url = "www.soft-grip.net/faq";
            Intent intent = new Intent(SettingsActivity.this, ActivityWebView.class);
            intent.putExtra("share", url);
            startActivity(intent);


            /*String url = "http://www.google.com";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);*/

            return false;
        }
    });

    Preference fb =  (Preference) findPreference("fb");
    fb.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

            try {
                Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("fb://page/503359346436234"));
                startActivity(intent);
            } catch(Exception e) {
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://www.facebook.com/Soft.Grip.Inc")));
            }

            return false;

        }
    });

    Preference twt =  (Preference) findPreference("twt");
    twt.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

            Intent intent = null;
            try {
                // get the Twitter app if possible
                getPackageManager().getPackageInfo("com.twitter.android", 0);
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("twitter://user?user_id=1588577185"));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            } catch (Exception e) {
                // no Twitter app, revert to browser
                intent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://twitter.com/SoftGripInc"));
            }
            startActivity(intent);

            return false;

        }
    });

    Preference sms = (Preference)findPreference("sms");
    sms.setOnPreferenceClickListener(new OnPreferenceClickListener() {

        @Override
        public boolean onPreferenceClick(Preference arg0) {

            Intent sendIntent= new Intent(Intent.ACTION_VIEW);
            sendIntent.putExtra("sms_body", "smsBody");
            sendIntent.setType("vnd.android-dir/mms-sms");
            startActivity(sendIntent);

            return false;
        }
    });

    Preference http =  (Preference) findPreference("http");
    http.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

            String url = "http://www.soft-grip.net";
            Intent i = new Intent(Intent.ACTION_VIEW);
            i.setData(Uri.parse(url));
            startActivity(i);

            return false;

        }
    });

    Preference merci =  (Preference) findPreference("deve");
    merci.setOnPreferenceClickListener(new OnPreferenceClickListener() {
        public boolean onPreferenceClick(Preference preference) {

            ShowDialog(SettingsActivity.this, "Merci", "Merci d'utiliser l'App Soft Grip!\nDéveloppeur : Soft Grip Inc. \nTous les droits réservés. \nSoft-Grip Inc © 2015 ", false);
            return false;

        }




        public void ShowDialog(Context context, String title, String message, Boolean status) {
    AlertDialog alertDialog = new AlertDialog.Builder(context).create();

    // Setting Dialog Title
    alertDialog.setTitle(null);

    // Setting Dialog Message
    alertDialog.setMessage(message);

    // Setting alert dialog icon
//  alertDialog.setIcon((status) ? R.drawable.success : R.drawable.fail);

    // Setting OK Button
    alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {


        }
    });

    // Showing Alert Message
    alertDialog.show();

}
    });


}

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            deleteDir(dir);
        }
    } catch (Exception e) {}
}

public static boolean deleteDir(File dir) {
    if (dir != null && dir.isDirectory()) {
        String[] children = dir.list();
        for (int i = 0; i < children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }
    return dir.delete();
}

Logcat:

android.view.InflateException: Binary XML file line #19: Error inflating class android.widget.CheckBox
    at android.view.LayoutInflater.createView(LayoutInflater.java:627)
    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:676)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:701)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:470)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:398)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
    at android.preference.Preference.onCreateView(Preference.java:531)
    at android.preference.Preference.getView(Preference.java:494)
    at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:222)
    at android.widget.AbsListView.obtainView(AbsListView.java:2351)
    at android.widget.ListView.makeAndAddView(ListView.java:1816)
    at android.widget.ListView.fillDown(ListView.java:697)
    at android.widget.ListView.fillFromTop(ListView.java:763)
    at android.widget.ListView.layoutChildren(ListView.java:1646)
    at android.widget.AbsListView.onLayout(AbsListView.java:2207)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1677)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1445)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1692)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1534)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1443)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
    at android.view.View.layout(View.java:15033)
    at android.view.ViewGroup.layout(ViewGroup.java:4799)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2143)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1854)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1062)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5998)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
    at android.view.Choreographer.doCallbacks(Choreographer.java:574)
    at android.view.Choreographer.doFrame(Choreographer.java:544)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5590)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1280)
    at com.android.into

【问题讨论】:

  • 否决这个问题的原因是什么?
  • 您是截断一行还是忘记关闭 PreferenceScreen 标签?
  • @PabloRivero nop 我没有忘记关闭它只是剪了几行来在这里发布代码:/比你
  • 我会在黑暗中拍摄,但这可能是因为字符串 Activer/Désactiver les notification 中的反斜杠。尝试更改文本并告诉我。
  • @UrielFrankel 我不这么认为,因为我添加了另一个活动和带有 CheckBoxPreference 的 xml 首选项,带有一个简单的文本没有任何工作和同样的问题,但是当我删除这个 CheckBoxPreference 时一切正常

标签: android xml checkbox


【解决方案1】:

支持库的23.2.0 版本中有一个bug 可能导致此问题。

这已在库的修订版 23.2.1 中得到修复。此修订说明了以下内容,我认为这是问题的根本原因,并解释了为什么它仅在运行 API 19 的手机上失败:

修复了 API 上 DrawableCompat.wrap() 和 LayerDrawable 中的异常 17 到 19 级。

【讨论】:

    【解决方案2】:

    查看您的代码,您是否尝试过从

    中删除“/”字符
    android:title="Activer/Désactiver les notifications" 
    

    编辑:我可能应该对此发表评论,但我还没有声誉。

    【讨论】:

    • 不是“/”而是关于支持库 23.2.0 它是在 Api 19 中运行应用程序的错误谢谢
    • 感谢您告诉我!
    猜你喜欢
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-29
    • 1970-01-01
    相关资源
    最近更新 更多