在片段中使用 (android:entries="@array/type") 的微调器上的错误标记异常

Bad token exception on spinner which uses (android:entries="@array/type") in fragment

当我在某些设备上 运行 我的代码时(比如 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);
});

我的片段仍然 运行ning 在弹出窗口后面,所以这个错误一定不会出现,因为它表明 activity 不是 运行ning。 请记住,它在最新设备上 运行ning OK。

尝试如下

View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.add_reminder, null);

View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.add_reminder, null);

我通过将我的 popup 的 XML 代码放在片段中并使其在点击时可见来解决了这个错误。通过这种方式,spinner 的代码在每个设备上都能很好地运行,这样 错误一定是在自定义弹出窗口中使用微调器的某个地方。