android - 使用微调器的错误 token 异常(android :entries ="@array/type") in fragment

标签 android android-spinner android-popupwindow

当我在某些设备(如 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/

相关文章:

android - 如何在 TextView 下方正确显示弹出菜单,类似于 Spinner?

android - 如何将自定义类 List 的属性绑定(bind)到 Android 中的 Spinner?

android - 包含微调器时无法向下滑动

android - 从 Fragment 显示弹出窗口

java - 让我的应用程序在启动完成开/关时启动

java - 如何设置 AppCompatSpinner 的文本?请查看详情

android - 如何用 ListPopupWindow 替换我的弹出菜单代码?

android - imageSwitcher 和内存错误

android - 如何在Android中将3gpp格式的音频文件转换为mp3

android - 如何使用 AsyncTask 获取 GPS 位置?