1

I get my app force close when I tap anywhere around the exact switch button itself.

Screenshot describing this: enter image description here tinypic / imgur

preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory
        android:title="@string/main_settings_title"
        android:key="pref_key_main_settings">
        <SwitchPreference
            android:key="pref_key_enable_clipboard_service"
            android:summary="@string/pref_summary_enable_clipboard_service"
            android:title="@string/pref_title_enable_clipboard_service"
            android:defaultValue="false" />
    </PreferenceCategory>
</PreferenceScreen>

Logcat upon error

07-10 20:36:48.154  23395-23395/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
    at android.widget.Switch.onPopulateAccessibilityEvent(Switch.java:545)
    at android.view.View.dispatchPopulateAccessibilityEventInternal(View.java:4621)
    at android.view.View.dispatchPopulateAccessibilityEvent(View.java:4611)
    at android.preference.TwoStatePreference.sendAccessibilityEvent(TwoStatePreference.java:197)
    at android.preference.SwitchPreference.onBindView(SwitchPreference.java:114)
    at android.preference.Preference.getView(Preference.java:463)
    at android.preference.PreferenceGroupAdapter.getView(PreferenceGroupAdapter.java:221)
    at android.widget.AbsListView.obtainView(AbsListView.java:2289)
    at android.widget.ListView.makeAndAddView(ListView.java:1775)
    at android.widget.ListView.fillDown(ListView.java:675)
    at android.widget.ListView.fillSpecific(ListView.java:1336)
    at android.widget.ListView.layoutChildren(ListView.java:1606)
    at android.widget.AbsListView.onLayout(AbsListView.java:2124)
    at android.view.View.layout(View.java:13842)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1535)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1448)
    at android.view.View.layout(View.java:13842)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13842)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1677)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1535)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1448)
    at android.view.View.layout(View.java:13842)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13842)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1886)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1707)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1010)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4236)
    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
    at android.view.Choreographer.doCallbacks(Choreographer.java:555)
    at android.view.Choreographer.doFrame(Choreographer.java:525)
    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
    at android.os.Handler.handleCallback(Handler.java:615)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4863)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
    at dalvik.system.NativeStart.main(Native Method)

I've googled many times and I've found only one same case so far here that someone suggests to use custom SwitchPreferences component to override onClick() method. But I don't know/understand how to do that.

Any information about this error is welcome. Code snippets are most welcome! I'm a newbie.

Community
  • 1
  • 1
Ilo
  • 35
  • 6
  • I can confirm the problem. Looks like a bug in Android IMHO. – Stan Jul 26 '15 at 19:50
  • Yes I agree with you. Did you find any workaround? Did you understand the solution suggested from thread that I mentioned? – Ilo Jul 28 '15 at 16:09
  • Yes, I suppose I understand that suggestion (I have created some custom preferences for myself) but it looks like an overkill and improper way of doing things. As far as the problem occurred only once in my Google reporting, I thinks it's of a minor importance and does not require any workaround. – Stan Jul 30 '15 at 09:52
  • How is that possible the error occurred only once? According to me, that will occur on every inaccurate tap in the settings switch area... Also could you direct me to the right place to understand how to create custom preferences? Cause I failed to understand that thread answer – Ilo Jul 30 '15 at 18:46
  • I got this error from Google reporting once (i.e. it happened on a user's device, not mine). You should extend a base preference class (`Preference` or `SwitchPreference`) and override some important methods. I believe there is a lot of examples in the Internet (for example, here is [one for a seekbar](http://robobunny.com/wp/2011/08/13/android-seekbar-preference/)). – Stan Aug 01 '15 at 17:41

1 Answers1

0

I had the same problem, and solved it by using a checkbox preference instead of a switch preference. From my code:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<CheckBoxPreference
    android:defaultValue="true"
    android:key="game_sound_switch"
    android:summary="play beeps and buzzes while in the game"
    android:title="Play game sounds" />

</PreferenceScreen>

The problem seems to be a bug in Android's accessibility feature. When you click outside of the switch button, it is supposed to flip the switch anyway (this is so people who have trouble aiming for the exact button will still be able to change the preference).

If you click on the link at the end of the first line in the logcat, you will see that the file Switch.java is defining a variable as null, then trying to call that same variable later.

at android.widget.Switch.onPopulateAccessibilityEvent(Switch.java:545)

So I guess use a checkbox preference until Android fixes this? Sorry I couldn't be more helpful.