单击Android 5.0时切换崩溃

时间:2014-11-18 06:28:01

标签: android nullpointerexception android-switch

在Android 5.0中点击我的应用程序中的开关时,应用程序崩溃并显示如下所示的logcat。 logcat不会在其中的任何位置引用我的代码,并且此开关在所有先前版本上都运行良好。除了背景颜色外,开关看起来不可见,只有在点击后才会崩溃。

经过测试,无论我是否定义了setOnCheckedChangeListener函数,都会发生同样的事情。即使交换机在布局中但从未在代码中,它仍然会在单击时崩溃。

其中一个开关:

<Switch
    android:layout_width="90dp"
    android:layout_height="wrap_content"
    android:textOff="Airborn"
    android:textOn="Direct Contact"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:id="@+id/switchWind"/>

以下是我如何引用它。

// Doesn't actually matter since same thing happens without this
Switch sWind = (Switch) findViewById(R.id.switchWind);
sWind.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    public void onCheckedChanged(CompoundButton v, boolean isChecked) {
        // Won't reach here
    }
});

logcat的:

11-17 22:09:18.722  30190-30190/com.egondev.android.patientzerotest E/InputEventReceiver﹕ Exception dispatching input event.
11-17 22:09:18.722  30190-30190/com.egondev.android.patientzerotest E/MessageQueue-JNI﹕ Exception in MessageQueue callback: handleReceiveCallback
11-17 22:09:18.735  30190-30190/com.egondev.android.patientzerotest E/MessageQueue-JNI﹕ java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
        at android.widget.Switch.hitThumb(Switch.java:694)
        at android.widget.Switch.onTouchEvent(Switch.java:711)
        at android.view.View.dispatchTouchEvent(View.java:8388)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
        at android.view.View.dispatchPointerEvent(View.java:8578)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:143)
        at android.os.Looper.loop(Looper.java:122)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-17 22:09:18.735  30190-30190/com.egondev.android.patientzerotest D/AndroidRuntime﹕ Shutting down VM
11-17 22:09:18.736  30190-30190/com.egondev.android.patientzerotest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.egondev.android.patientzerotest, PID: 30190
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.drawable.Drawable.getPadding(android.graphics.Rect)' on a null object reference
        at android.widget.Switch.hitThumb(Switch.java:694)
        at android.widget.Switch.onTouchEvent(Switch.java:711)
        at android.view.View.dispatchTouchEvent(View.java:8388)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2430)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2119)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2314)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1692)
        at android.app.Activity.dispatchTouchEvent(Activity.java:2739)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2275)
        at android.view.View.dispatchPointerEvent(View.java:8578)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4021)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3887)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3578)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3635)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3502)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3468)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3476)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3449)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5701)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5675)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5646)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5791)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:143)
        at android.os.Looper.loop(Looper.java:122)
        at android.app.ActivityThread.main(ActivityThread.java:5221)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

4 个答案:

答案 0 :(得分:9)

这似乎是Android 5中的一个已知问题。

请参阅以下链接:

https://code.google.com/p/android-developer-preview/issues/detail?id=1704

答案 1 :(得分:1)

只需改变拇指并跟踪开关的图像。

答案 2 :(得分:0)

我在使用Android 5.0.2的索尼平板电脑上遇到了同样的问题 - 当活动风格为NoTitleBar / FullScreen时,Switch会崩溃我的应用程序。

对我来说效果很好的是在代码中而不是在清单中设置FullScreen模式。

View decorView = getWindow().getDecorView();
int uiOptions = View.SYSTEM_UI_FLAG_FULLSCREEN;
decorView.setSystemUiVisibility(uiOptions);

如果我不弄乱某些东西,结果与usuall相同,但没有崩溃。

答案 3 :(得分:0)

我遇到了同样的问题。我通过改变来解决它:

android:thumb="@null"

为:

android:thumb="@drawable/transparent_thumb"
android:track="@drawable/transparent_track"

transparent_thumb&amp; transparent_track是两个透明的png文件。它们的尺寸与您定制的Switch相匹配。