当我点击确切的开关按钮本身周围的任何位置时,我的应用程序强制关闭。
首选项.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() 方法。但我不知道/不明白如何做到这一点。
欢迎提供有关此错误的任何信息。代码片段是最受欢迎的!我是新手。