PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 NullPointerException_移动开发_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > 移动开发 > PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 NullPointerException

PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 NullPointerException

 2013/8/19 16:29:18  娄立军  博客园  我要评论(0)
  • 摘要:错误1:java.lang.NullPointerExceptionatandroid.widget.PopupWindow$1.onScrollChanged(PopupWindow.java:127)atandroid.view.ViewTreeObserver.dispatchOnScrollChanged(ViewTreeObserver.java:607)atandroid.view.ViewRoot.draw(ViewRoot.java:1453)atandroid.view
  • 标签:KEY view 错误 NullPointerException

错误1:

java.lang.NullPointerException
at android.widget.PopupWindow$1.onScrollChanged(PopupWindow.java:127)
at android.view.ViewTreeObserver.dispatchOnScrollChanged(ViewTreeObserver.java:607)
at android.view.ViewRoot.draw(ViewRoot.java:1453)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1335)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1991)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:4385)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
at dalvik.system.NativeStart.main(Native Method)

这个错误经常出现在Gingerbread或更早的版本,3.0以下。是系统的bug

解决方案:

new PopupWindow之后加上如下的方法进行检查即可,为了兼容3.0以下版本,这个操作还是必要的

private void fixPopupWindow(final PopupWindow window) {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        try {
            final Field fAnchor = PopupWindow.class
                    .getDeclaredField("mAnchor");
            fAnchor.setAccessible(true);
            Field listener = PopupWindow.class
                    .getDeclaredField("mOnScrollChangedListener");
            listener.setAccessible(true);
            final ViewTreeObserver.OnScrollChangedListener originalListener = (ViewTreeObserver.OnScrollChangedListener) listener
                    .get(window);
            ViewTreeObserver.OnScrollChangedListener newListener = new ViewTreeObserver.OnScrollChangedListener() {
                @Override
                public void onScrollChanged() {
                    try {
                        WeakReference<View> mAnchor = (WeakReference<View>) fAnchor.get(window);
                        if (mAnchor == null || mAnchor.get() == null) {
                            return;
                        } else {
                            originalListener.onScrollChanged();
                        }
                    } catch (IllegalAccessException e) {
                        e.printStackTrace();
                    }
                }
            };
            listener.set(window, newListener);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

参考:

https://github.com/JakeWharton/ActionBarSherlock/issues/752

https://groups.google.com/forum/?fromgroups=#!msg/osmeditor4android/RsHYoY6XqlU/-WGSKWm5OMAJ

 

错误2:

java.lang.NullPointerException
 at android.widget.PopupWindow$PopupViewContainer.dispatchKeyEvent(PopupWindow.java:1426)
 at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2627)
 at android.view.ViewRoot.deliverKeyEvent(ViewRoot.java:2592)
 at android.view.ViewRoot.handleMessage(ViewRoot.java:1881)
 at android.os.Handler.dispatchMessage(Handler.java:99)
 at android.os.Looper.loop(Looper.java:130)
 at android.app.ActivityThread.main(ActivityThread.java:3687)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:507)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
 at dalvik.system.NativeStart.main(Native Method)

解决方法:创建PopupWindow 的时候不调用 setFocusable(true),在showAtLocation后在调用setFocusable(true);并且在 dismiss的时候 调用setFocusable(false)

参考:

http://stackoverflow.com/questions/7768728/popupwindow-crash-on-dispatch-event

http://www.eoeandroid.com/thread-109193-1-1.html

发表评论
用户名: 匿名