我有一个应用程序,它使用popupWindow
进行用户聊天。它是从Fragment
启动的,所提供的context
是getActivity()
。我将输入popupWindow
的multilineEditText
,大约5分钟后,我将经常遇到以下错误:
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
问题的示例代码:
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;
}
}
});
}
}
发布于 2022-05-18 16:25:10
尽管在我所经历的核心问题的原因上大错特错,但我想我还是把这个问题留给那些和我一样不善于阅读堆栈痕迹的人吧。
这可能不是完美的解决方案,但是,感谢Mike的评论,我能够通过禁用xml文件中EditText
的建议弹出来解决眼前的问题,如下所示:
android:inputType="textNoSuggestions"
否则,解决方案将是将PopupWindow
替换为Dialog
或Activity
。
https://stackoverflow.com/questions/72281047
复制相似问题