当我在某些设备(如 MI note 4)上运行我的代码时,出现异常:
android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@7989790 is not valid; is your activity running?
但是当我在高速和最新的移动设备(MI note 5 pro)上运行它时它工作正常。我无法理解这里的错误,请指导我,谢谢。
这是微调器的 XML:-
<Spinner
android:id="@+id/type_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dp_10"
android:entries="@array/type" />
和我的字符串数组:
<string-array name="type">
<item>Every Month</item>
<item>Every Year</item>
<item>Once</item>
</string-array>
add_reminder.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.add_reminder, null);
final PopupWindow pw = new PopupWindow(view1, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
pw.setFocusable(true);
pw.setTouchable(true);
pw.showAtLocation(view, Gravity.CENTER, 0, 0);
final Spinner type = view1.findViewById(R.id.type_spinner);
});
我的 fragment 仍在弹出窗口后面运行,所以这个错误一定不会出现,因为它表明 Activity 没有运行。 请记住它在最新设备上运行正常。
最佳答案
尝试如下
View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.add_reminder, null);
到
View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.add_reminder, null);
关于android - 使用微调器的错误 token 异常(android :entries ="@array/type") in fragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54529732/