spinner上的错误令牌异常在片段中使用(android:entries =“@ array / type”)
作者:互联网
当我在某些设备上运行我的代码时(如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);
});
我的片段仍然在弹出窗口后面运行,因此该错误一定不会出现,因为它表明活动没有运行.
并记住它在最新设备上正常运行.
解决方法:
尝试如下
View view1 = LayoutInflater.from(getActivity()).inflate(R.layout.add_reminder, null);
至
View view1 = LayoutInflater.from(view.getContext()).inflate(R.layout.add_reminder, null);
标签:android,android-spinner,android-popupwindow 来源: https://codeday.me/bug/20190710/1424373.html