gpt4 book ai didi

android - SwitchCompat 抛出错误

转载 作者:IT老高 更新时间:2023-10-28 22:24:14 26 4
gpt4 key购买 nike

我正在尝试在我的 fragment 中使用 switchcompat。最小支持的 API 为 14,最大为 21。我正在尝试应用 Material View 来切换所有 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

异常(exception):

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'])
}

最佳答案

SwitchCompat 要求您设置 android:textOnandroid: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" />

关于android - SwitchCompat 抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26553930/

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