我尝试以编程方式添加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);
答案 0 :(得分:8)
使用活动上下文而非应用程序上下文来扩充视图,以便例如主题工作。
崩溃是因为textOn
和textOff
标签为空,通常会从主题中获取默认值。
答案 1 :(得分:1)
尝试创建这样的SwitchCompat时遇到相同的问题:
SwitchCompat switchCompat = new SwitchCompat(getContext());
switchCompat.setText(...);
通过将其扩展为XML布局来解决:
layoutInflater.inflate(R.layout.view_switch_compat, parent, false);
并且view_switch_compat.xml包含:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.SwitchCompat
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/switchCompat"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
希望它对某人有帮助:-)