1

当我点击确切的开关按钮本身周围的任何位置时,我的应用程序强制关闭。

描述此的屏幕截图: tinypic / imgur在此处输入图像描述

首选项.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

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)

我用谷歌搜索了很多次,到目前为止我只发现了一个相同的案例 有人建议使用自定义 SwitchPreferences 组件来覆盖 onClick() 方法。但我不知道/不明白如何做到这一点。

欢迎提供有关此错误的任何信息。代码片段是最受欢迎的!我是新手。

4

1 回答 1

0

我有同样的问题,并通过使用复选框首选项而不是开关首选项来解决它。从我的代码:

<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>

这个问题似乎是 Android 的辅助功能中的一个错误。当您在开关按钮外部单击时,无论如何都应该翻转开关(这样在瞄准确切按钮时遇到困难的人仍然可以更改偏好)。

如果单击 logcat 中第一行末尾的链接,您将看到文件 Switch.java 将变量定义为 null,然后尝试稍后调用相同的变量。

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

所以我想在Android修复这个问题之前使用复选框首选项?抱歉,我无法提供更多帮助。

于 2016-04-02T06:27:52.840 回答