PopupWindow錯誤:PopupWindow$1.onScrollChanged 出現 NullPointerException和PopupViewContainer.dispatchKeyEv

錯誤1:html

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如下。是系統的bugjava

解決方案:android

new PopupWindow以後加上以下的方法進行檢查便可,爲了兼容3.0如下版本,這個操做仍是必要的git

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();
        }
    }
}

參考:github

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

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

 

錯誤2:oop

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

參考:google

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

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

相關文章
相關標籤/搜索