我正在尝试在我的片段中使用switchcompat。 min支持的API是14,最大值是21.我正在尝试应用材质视图来切换所有pre lollipop android版本。但是当使用下面的代码时,我收到一个错误: 我该如何纠正?
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
在抛出异常的java代码中膨胀:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.fragment_scheduler_ui, container, false);
参考: http://android-developers.blogspot.in/2014/10/appcompat-v21-material-design-for-pre.html
例外:
10-24 14:15:15.880 1561-1561/com.stackoverflow.ranjith.androidprojdel E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.stackoverflow.ranjith.androidprojdel, PID: 1561
java.lang.NullPointerException
at android.text.Layout.getDesiredWidth(Layout.java:67)
at android.support.v7.widget.SwitchCompat.makeLayout(SwitchCompat.java:570)
at android.support.v7.widget.SwitchCompat.onMeasure(SwitchCompat.java:495)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
at android.view.View.measure(View.java:16497)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
at android.view.View.measure(View.java:16497)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
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:5017)
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:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
的build.gradle:
dependencies {
compile 'com.android.support:cardview-v7:+'
compile 'com.android.support:recyclerview-v7:+'
compile 'com.android.support:palette-v7:+'
compile 'com.android.support:support-v4:+'
compile "com.android.support:appcompat-v7:21.0.+"
compile fileTree(dir: 'libs', include: ['*.jar'])
}
答案 0 :(得分:54)
SwitchCompat
要求您设置android:textOn
和android:textOff
值,除非您设置app:showText="false"
:
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="ON"
android:textOff="OFF" />
或(假设您的XML文件顶部有xmlns:app="http://schemas.android.com/apk/res-auto"
)
<android.support.v7.widget.SwitchCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:showText="false" />
答案 1 :(得分:0)
发生相同的错误,最终什么也没找到,也落在了这里。同事为我建议了一件小事来解决此异常。所以我想把这个留在这里:
person
或SwitchMaterial
在内部使用自定义SwitchCompat
。因此,默认代码在未提供时总是用0覆盖defStyleAttr。这会覆盖父组件中的样式,然后发生崩溃。
从以下位置更改我的构造函数:
defStyleAttr
到
class MySwitch(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : SwitchMaterial(context, attrs, defStyleAttr)
为我解决了问题