gpt4 book ai didi

java - 以编程方式添加 SwitchCompat 错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:42:26 25 4
gpt4 key购买 nike

我正在尝试以编程方式添加 android.support.v7.widget.SwitchCompat,但出现此错误:

java.lang.NullPointerException: Attempt to invoke interface method 'int java.lang.CharSequence.length()' on a null object reference
at android.text.StaticLayout.<init>(StaticLayout.java:422)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:602)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:526)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.support.v7.widget.Toolbar.measureChildCollapseMargins(Toolbar.java:1218)
at android.support.v7.widget.Toolbar.onMeasure(Toolbar.java:1326)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.support.design.widget.AppBarLayout.onMeasure(AppBarLayout.java:199)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(CoordinatorLayout.java:668)
at android.support.design.widget.AppBarLayout$Behavior.onMeasureChild(AppBarLayout.java:888)
at android.support.design.widget.AppBarLayout$Behavior.onMeasureChild(AppBarLayout.java:652)
at android.support.design.widget.CoordinatorLayout.onMeasure(CoordinatorLayout.java:733)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1458)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:746)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:629)
at android.view.View.measure(View.java:18799)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:194)
at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2899)
at android.view.View.measure(View.java:18799)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2108)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1224)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1460)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1115)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6023)
at android.view.Choreographer$CallbackRecord.run(Chore

但是当我使用 Switch 时一切正常。为什么会这样?这是我的代码:

SwitchCompat switchBox = (SwitchCompat) LayoutInflater.from(getApplicationContext()).inflate(R.layout.checkbox, null);
switchBox.setId(i);
Typeface typeface = Typeface.createFromAsset(getAssets(), "iransansfont.ttf");
switchBox.setText(convert_num((nodesModelArrayList.get(i).type ? "بیمارستان " : "مطب ") + nodesModelArrayList.get(i).node_name + ": " + nodesModelArrayList.get(i).stat));
switchBox.setTypeface(typeface);
switchBox.setOnCheckedChangeListener(onCheckedChangeListener);
linearLayout.addView(switchBox);
switchBox.setChecked(true);

最佳答案

使用 Activity 上下文而不是应用程序上下文来膨胀 View ,例如主题工作。

崩溃是因为 textOntextOff 标签为 null,通常会从主题中获取它们的默认值。

关于java - 以编程方式添加 SwitchCompat 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37992137/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com