app crashes when spinner is pressed inside popupwindow which is located in fragment

爱⌒轻易说出口 提交于 2019-12-20 06:13:33

问题


i have a fragment with a button. The button is pressed and if the user credentials entered match a valid user(the input fields ara being checked requests to rest api) a popupwindow is displayed with a spinner, a textview and an editext.

in volley onresponse createPopUpWindow method is called

onResponse()

public void onResponse(String response) {
                    try {
                        JSONObject jsonObject = new JSONObject(response);
                        String success = jsonObject.getString(AppConfig.TAG_ERROR);
                        if (success.equals("true")){
                           errorresponseTtv.setText(jsonObject.getString(AppConfig.TAG_ERROR_MSG));
                        }
                        else{
                           errorresponseTtv.setText("Success!:\n User-> " + jsonObject.getString(AppConfig.TAG_USERNAME) + ", password-> " + jsonObject.getString(AppConfig.TAG_PASSWORD) + ", email-> " + jsonObject.getString(AppConfig.TAG_EMAIL));
                            inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                            View layout = inflater.inflate(R.layout.popup_login, null);

                            createPopUpWindow(layout);
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }

createPopUpWindow()

private void createPopUpWindow(final View layout) {
            popupWindow = new PopupWindow(layout,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    ViewGroup.LayoutParams.WRAP_CONTENT,
                    true);
            firmSpnr = (Spinner) layout.findViewById(R.id.firmSpnr);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item, arrayList);
            firmSpnr.setAdapter(arrayAdapter);
            popupWindow.setOutsideTouchable(true);
            popupWindow.setBackgroundDrawable(new ShapeDrawable());
            popupWindow.showAtLocation(layout, Gravity.CENTER, 0, 0);
            popupWindow.update();
}

the popupWindow is displayed as it is expected but the problem occurs when i press the spinner.

The log message is "Unable to add window -- token android.view.ViewRootImp" is your activity running? i have searched about any similar issues others have faced and i found out that i must defer showing the popup until after all the lifecycle methods are called. Can anyone provide a solution on how to fix the error?

log error messages

FATAL EXCEPTION: main
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime: Process: com.votingsystem.tsiro.votingsystem, PID: 32577
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@421769f8 is not valid; is your activity running?
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:925)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.internal.widget.AppCompatPopupWindow.showAsDropDown(AppCompatPopupWindow.java:78)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompatKitKat.showAsDropDown(PopupWindowCompatKitKat.java:30)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompat$KitKatPopupWindowImpl.showAsDropDown(PopupWindowCompat.java:92)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v4.widget.PopupWindowCompat.showAsDropDown(PopupWindowCompat.java:171)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.ListPopupWindow.show(ListPopupWindow.java:654)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.AppCompatSpinner$DropdownPopup.show(AppCompatSpinner.java:766)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.support.v7.widget.AppCompatSpinner.performClick(AppCompatSpinner.java:424)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.view.View$PerformClick.run(View.java:18446)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Handler.handleCallback(Handler.java:733)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:95)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:136)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5146)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at java.lang.reflect.Method.invokeNative(Native Method)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:515)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
11-03 18:56:17.703 32577-32577/com.votingsystem.tsiro.votingsystem E/AndroidRuntime:     at dalvik.system.NativeStart.main(Native Method)

回答1:


try this in OnResponse else condition

 layout.post(new Runnable() {
        @Override
        public void run() {
            createPopUpWindow(layout);
        }
    });

instead of createPopUpWindow(layout);




回答2:


Similar issue was reported on google https://code.google.com/p/android/issues/detail?id=202246

But after release of Android Support Library, revision 23.2.1 (March 2016) This issue has been resolved.

Fixed a compatibility crash with AppCompatSpinner on API level 21 and below

update Support Library to Android Support Library to 23.2.1



来源:https://stackoverflow.com/questions/33484491/app-crashes-when-spinner-is-pressed-inside-popupwindow-which-is-located-in-fragm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!