zoukankan      html  css  js  c++  java
  • PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 NullPointerException【转载】

    PopupWindow错误:PopupWindow$1.onScrollChanged 出现 NullPointerException和PopupViewContainer.dispatchKeyEvent 出现 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

  • 相关阅读:
    (网页)中的简单的遮罩层
    (后端)shiro:Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
    (网页)jQuery的时间datetime控件在AngularJs中使用实例
    Maven Myeclipse 搭建项目
    MyBatis 环境搭建 (一)
    java 常用方法
    XML 基础
    JS BOM
    js 事件
    js 的使用原则
  • 原文地址:https://www.cnblogs.com/xinye/p/3642678.html
Copyright © 2011-2022 走看看