首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Android getActivity()上下文随着时间的推移在PopupWindow中过时

Android getActivity()上下文随着时间的推移在PopupWindow中过时
EN

Stack Overflow用户
提问于 2022-05-17 21:52:15
回答 1查看 63关注 0票数 0

我有一个应用程序,它使用popupWindow进行用户聊天。它是从Fragment启动的,所提供的contextgetActivity()。我将输入popupWindowmultilineEditText,大约5分钟后,我将经常遇到以下错误:

代码语言:javascript
运行
复制
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@d1cf1ec is not valid; is your activity running?
    at android.view.ViewRootImpl.setView(ViewRootImpl.java:1147)
    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:471)
    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:95)
    at android.widget.PopupWindow.invokePopup(PopupWindow.java:1621)
    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1375)
    at android.widget.PopupWindow.showAtLocation(PopupWindow.java:1341)
    at android.widget.Editor$PinnedPopupWindow.updatePosition(Editor.java:3817)
    at android.widget.Editor$PinnedPopupWindow.show(Editor.java:3773)
    at android.widget.Editor$SuggestionsPopupWindow.show(Editor.java:4287)
    at android.widget.Editor.replace(Editor.java:587)
    at android.widget.-$$Lambda$DZXn7FbDDFyBvNjI-iG9_hfa7kw.run(Unknown Source:2)
    at android.os.Handler.handleCallback(Handler.java:883)
    at android.os.Handler.dispatchMessage(Handler.java:100)
    at android.os.Looper.loop(Looper.java:237)
    at android.app.ActivityThread.main(ActivityThread.java:8167)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:496)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1100)

我知道这对解释错误并没有太大帮助,但据我所知,这是getActivity返回null和我的popupWindow强行脱离缺乏context的一个问题。有没有办法避免和/或解决这个问题?

下面是一些帮助可视化popupWindow问题的示例代码:

代码语言:javascript
运行
复制
if(getActivity() != null) {

        //Retrieve layout inflater
        LayoutInflater layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

        if (layoutInflater != null) {

            //Inflate custom popup layout
            View inflatedView = layoutInflater.inflate(R.layout.popup_chat_layout, null, false);

            writeMessageEditText = inflatedView.findViewById(R.id.writeMessageEditText);

            chatLayoutManager = new LinearLayoutManager(getActivity());
            //Read from bottom-up
            chatLayoutManager.setStackFromEnd(true);
            chatMessagesRecycler.setLayoutManager(chatLayoutManager);

            popupWindow = new PopupWindow(inflatedView, displayWidth, displayHeight, true);

            popupWindow.setAnimationStyle(R.style.PopupAnimation);

            popupWindow.setInputMethodMode(PopupWindow.INPUT_METHOD_NEEDED);
            popupWindow.setHeight(WindowManager.LayoutParams.MATCH_PARENT);

            popupWindow.showAtLocation(v, Gravity.BOTTOM, 0, 0);

            //Cap message lines - the purpose is not to cap characters, but ONLY to cap length
            writeMessageEditText.addTextChangedListener(new TextWatcher() {
                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(Editable s) {
                    if (writeMessageEditText.getLayout().getLineCount() > 150) {
                        if (writeMessageEditText.getText() != null) {
                            writeMessageEditText.getText().delete(writeMessageEditText.getText().length() - 1, writeMessageEditText.getText().length());
                        }
                        Toast.makeText(getActivity(), "Message may not exceed 150 lines", Toast.LENGTH_SHORT).show();
                    }
                }
            });

            //This was implemented during testing - possible to remove but fine for now
            dismissChatButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    resetAll();
                    previousChat = null;
                    if(popupWindow != null) {
                        popupWindow.dismiss();
                        popupWindow = null;
                    }
                }
            });

        }

    }
EN

回答 1

Stack Overflow用户

发布于 2022-05-18 16:25:10

尽管在我所经历的核心问题的原因上大错特错,但我想我还是把这个问题留给那些和我一样不善于阅读堆栈痕迹的人吧。

这可能不是完美的解决方案,但是,感谢Mike的评论,我能够通过禁用xml文件中EditText的建议弹出来解决眼前的问题,如下所示:

代码语言:javascript
运行
复制
android:inputType="textNoSuggestions"

否则,解决方案将是将PopupWindow替换为DialogActivity

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72281047

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档