SwitchPreference(PreferenceAtivity) - Android NULL

时间:2014-09-26 09:38:31

标签: android android-activity nullpointerexception

我有一个屏幕首选项,因此,使用PreferenceActivity,我已经构建并运行了所有内容,但是通过单击文本SwitchPreference(即行中的任何位置,而不是相同的),手机(版本4.1.2)他用Null Pointer Excetion突然爆发,但是如果点击相同的swtich工作正常。

这可能是什么?

错误:

    E/AndroidRuntime﹕ FATAL EXCEPTION: main
       java.lang.NullPointerException
    at android.widget.Switch.onPopulateAccessibilityEvent(Switch.java:550)
    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:2308)
    at android.widget.ListView.makeAndAddView(ListView.java:1769)
    at android.widget.ListView.fillDown(ListView.java:672)
    at android.widget.ListView.fillSpecific(ListView.java:1330)
    at android.widget.ListView.layoutChildren(ListView.java:1600)
    at android.widget.AbsListView.onLayout(AbsListView.java:2143)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1680)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1538)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1451)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1680)
    at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1669)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1453)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1680)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1538)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1451)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1680)
    at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1538)
    at android.widget.LinearLayout.onLayout(LinearLayout.java:1451)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
    at android.view.View.layout(View.java:13850)
    at android.view.ViewGroup.layout(ViewGroup.java:4364)
    at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1894)
    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1710)
    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1013)
    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4245)
    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:153)
    at android.app.ActivityThread.main(ActivityThread.java:5071)
    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)

1 个答案:

答案 0 :(得分:2)

将此添加到您的自定义switchPrefrences组件

@Override protected void onClick() { // Log.e("CustomSwitchPreferences", "onClick called"); }

将其留空并且不要调用super.onClick();

这会使你的onPrefrenceChange不受点击影响(切换操作不会受到影响,只会对行进行点击)