我有一个从片段调用的PopupWindow,除了我的Spinner,一切都有效.当我点击我的Spinner时它会崩溃并给出错误:

“android.view.WindowManager $BadTokenException:无法添加窗口 – 令牌android.view.ViewRootImpl$W@421a46d8无效;您的活动是否在运行?”

我的代码调用片段中的弹出窗口:

ManagerPopUp mManagerPopUp = new ManagerPopUp(getActivity());
mManagerPopUp.showPopUpAddEvent();

我在ManagerPopUp中的代码显示Popup:

public class ManagerPopUp {

    private static final String TAG = "PopUpManager";
    private Context mContext;

    public ManagerPopUp(Context mContext){
        this.mContext = mContext;
    }

    public PopupWindow showPopUpAddEvent(){
            final PopupWindow popUp = new PopupWindow(mContext);

            LayoutInflater inflater = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View layout = inflater.inflate(R.layout.popup_add_event,null);

            //Code for popup event
            TextView txtRoom,txtTitle,txtEnd,txtStart;
            ToggleButton tgRepeateWeekly,tgAddReminder;
            Button btConfirme,btCancel;

            txtTitle = (TextView)layout.findViewById(R.id.txt_lesson_title_event);
            txtRoom = (TextView)layout.findViewById(R.id.txt_lesson_room_event);
            txtStart = (TextView)layout.findViewById(R.id.txt_lesson_start_hour_event);
            txtEnd = (TextView)layout.findViewById(R.id.txt_lesson_end_hour_event);
            final Spinner spSelectCalendar = (Spinner)layout.findViewById(R.id.sp_select_calendar);
            Spinner spSelectReminder = (Spinner)layout.findViewById(R.id.sp_select_reminder_time);
            tgRepeateWeekly = (ToggleButton)layout.findViewById(R.id.tg_repeate_weekly); 
            tgAddReminder = (ToggleButton)layout.findViewById(R.id.tg_add_reminder);
            btConfirme = (Button)layout.findViewById(R.id.bt_confirme);
            btCancel = (Button)layout.findViewById(R.id.bt_cancel);

            /**************************************************************************************************************
            *
            *   Initializations
            *
            ***************************************************************************************************************/


            HandlerCalendarProvider mCalendarProvider = new HandlerCalendarProvider(mContext);
            final ArrayList<ClassCalendar> mCalendarsList = mCalendarProvider.getAllCalendars();
            AdapterCalendarSpinner mAdapterCalendarSpinner = 
                    new AdapterCalendarSpinner(mContext,mCalendarsList);

            spSelectCalendar.setAdapter(mAdapterCalendarSpinner);


            popUp.setContentView(layout);
            popUp.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
            popUp.setHeight(LinearLayout.LayoutParams.MATCH_PARENT);
            popUp.setFocusable(true);

            // Some offset to align the popup a bit to the left,and a bit down,relative to button's position.
            popUp.setBackgroundDrawable(new BitmapDrawable(mContext.getResources(),BitmapFactory.decodeResource(mContext.getResources(),R.color.color_popup_background))); //PopUpBackground
            popUp.showAtLocation(layout,Gravity.NO_GraviTY,0);
            return popUp;
        }
}

我的微调适配器:

public class AdapterCalendarSpinner extends BaseAdapter implements SpinnerAdapter {

    private Context mContext;
    private ArrayList<ClassCalendar> mCalendarsList;

    public AdapterCalendarSpinner(Context mContext,ArrayList<ClassCalendar> mCalendarsList) {

        this.mContext = mContext;
        this.mCalendarsList = mCalendarsList;
    }

    @Override
    public int getCount() {
        return mCalendarsList.size();
    }

    @Override
    public ClassCalendar getItem(int position) {
        return mCalendarsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position,View convertView,ViewGroup parent) {

        ClassCalendar o = mCalendarsList.get(position);

        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.sp_item_calendar_selected,null);

        LinearLayout llCalColor = (LinearLayout)convertView.findViewById(R.id.ll_calendar_selected_color);
        TextView txtCalName = (TextView)convertView.findViewById(R.id.txt_calendar_selected_name);
        TextView txtCalEmail = (TextView)convertView.findViewById(R.id.txt_calendar_selected_email);

        llCalColor.setBackgroundColor(o.getCalColor());
        txtCalName.setText(o.getCalName());
        txtCalEmail.setText(o.getCalEmail());
        txtCalName.setTextColor(Color.WHITE);
        txtCalEmail.setTextColor(Color.WHITE);

        return convertView;
    }

    @Override
    public View getDropDownView(int position,ViewGroup parent) {

        ClassCalendar o = mCalendarsList.get(position);

        LayoutInflater vi = (LayoutInflater)mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = vi.inflate(R.layout.sp_item_calendar,null);

        //convertView = LayoutInflater.from(mContext).inflate(R.layout.sp_item_calendar,null);

        LinearLayout llCalColor = (LinearLayout)convertView.findViewById(R.id.ll_calendar_color);
        TextView txtCalName = (TextView)convertView.findViewById(R.id.txt_calendar_name);
        TextView txtCalEmail = (TextView)convertView.findViewById(R.id.txt_calendar_email);

        llCalColor.setBackgroundColor(o.getCalColor());
        txtCalName.setText(o.getCalName());
        txtCalEmail.setText(o.getCalEmail());

        return convertView;
    }
}

查看它在我的应用程序中的显示方式:

单击My Spinner(图像中的红色标签)时出现的错误:

09-19 11:35:23.312: E/AndroidRuntime(2933): FATAL EXCEPTION: main

    09-19 11:35:23.312: E/AndroidRuntime(2933): Process: com.loopbug.ritmossegredosgym,PID: 2933
    09-19 11:35:23.312: E/AndroidRuntime(2933): android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@421a46d8 is not valid; is your activity running?
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.widget.PopupWindow.invokePopup(PopupWindow.java:1019)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:925)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.widget.ListPopupWindow.show(ListPopupWindow.java:625)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.widget.Spinner$DropdownPopup.show(Spinner.java:1112)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.widget.Spinner.performClick(Spinner.java:658)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.view.View$PerformClick.run(View.java:18446)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.os.Handler.handleCallback(Handler.java:733)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.os.Handler.dispatchMessage(Handler.java:95)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.os.Looper.loop(Looper.java:136)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at android.app.ActivityThread.main(ActivityThread.java:5137)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at java.lang.reflect.Method.invokeNative(Native Method)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at java.lang.reflect.Method.invoke(Method.java:515)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:611)
    09-19 11:35:23.312: E/AndroidRuntime(2933):     at dalvik.system.NativeStart.main(Native Method)

解决方法

final PopupWindow popUp = new PopupWindow(mContext);

检查你的背景.它是空的吗?

编辑:如果在对话框中显示微调器,则将其添加到微调器

android:spinnerMode="dialog"

如果您动态创建微调器:

Spinner spinner = new Spinner(context,Spinner.MODE_DIALOG);

android – 弹出窗口中Spinner出错,单击时崩溃的更多相关文章

  1. Android Spinner下划线颜色

    我可以使用style=“@style/Base.Widget.AppCompat.Spinner.Underlined”在spinner中添加下划线.如何仅使用样式更改下划线的颜色?我不想使用任何可绘制的文件来改变这一点.使用上面的样式,当用户点击它时,它只有下划线.它在正常状态下不会改变下划线的颜色.解决方法默认情况下,Spinner将在AppTheme中使用通过android:textColo

  2. android – 如何通过文本内部设置微调器选择

    我准备了一个表单,我必须使用相同的页面添加详细信息和编辑详细信息.添加详细信息时,所有字段都将为空,并且微调器选择将设置为“无选择”.现在我想设置我将从前一个活动传递的项目的微调器选择.怎么实现这个?由于spinner没有任何类似的方法,setSelection;还是有其他办法,我可以实现这个机制……有人请帮助我……解决方法这就是我所做的,它似乎工作正常

  3. android – Spinner图形bug API 21

    解决方法我设法以两种不同的方式解决这个问题:>为您的微调器设置样式:也许预定义样式的背景颜色足以让你.如果不试试>创建一个具有圆角半径的可绘制形状:并将其设置为popupBackground到您的微调器希望这有用!

  4. Android中的Spinner主题很暗

    我试图奇怪地改变工具栏微调器下拉主题这没有发生.当我点击微调器时,它总是会变暗.我想背景为灰色,文字为黑色.我没有任何动作栏.我通过工具栏设置一切.因此我尝试了以下方法:风格xml:更新:添加了以下主题:并将其应用于微调器:使用上面的设置,我可以更改下拉背景颜色但不能在将主题应用到微调器之前,我不能对选择产生涟漪效应吗?解决方法只需将以下内容添加到您的Spinner,就像对工具栏一样:所以你的最终Spinner看起来像这样:

  5. 通过编辑文本在android中动态填充Spinner

    我有一个微调器,其中的少量值来自strings.xml,我想使用Button的onclick事件从编辑文本中动态填充该微调器,但不知何故它不起作用,我每次都得到强制关闭.任何帮助将不胜感激.Logcat错误:–解决方法您正在尝试在从资源创建的适配器中添加项目,因此它将为您提供不受支持的结果.要解决此问题,您可以创建一个空白列表,然后从资源中添加列表中的项目.为此你可以使用Arrays.asList

  6. Spinner在android中不起作用

    Spinner无法正常工作.这是我的代码:解决方法你犯的唯一错误是你设置了不同的事件监听器..ListenerOnItemClickListener适用于ListView..对于Spinner,你必须设置OnItemSelectedListener..尝试将OnItemClickListener替换为OnItemSelectedListener,如:

  7. 如何在spinner android中删除默认值?

    我使用JSON从数据库中获取值并将它们插入到微调器中.此过程已成功运行.但是当我点击旋转器时,我收到了两次Spinner提示;如何从微调器中删除第一个默认值…

  8. Android Spinner – 如果选项长度不同,如何将下拉箭头定位为尽可能接近文本?

    我的微调器中的选项具有不同的长度,并且当前下拉箭头基于最长选项位于远离右侧,如下面的屏幕截图所示.是否可以移动下拉箭头,以便根据当前选择的选项动态定位?

  9. Android自定义弹出菜单带开关

    如何在弹出菜单中自定义menuItem?我需要第一个menuitem的开关.这是我到目前为止所得到的:menu.xml文件menu_popup.xml活动代码段解决方法您可以使用popupwindow,因为它允许使用自定义布局.并在ur活动中实现此方法:

  10. android – 如何在外面点击时使用和关闭PopupWindow?

    我目前正在使用PopupWindow类设计简单的提示弹出窗口.点击内容后,我可以关闭弹出窗口.然而,外部触摸事件由下划线布局消耗.如何消耗外部触摸,以便在外部点击时我仍然关闭弹出窗口但是没有触摸事件被发送到我的PopupWindow下面的布局?在深入了解PopupWindow和ListPopupWindow后,我发现了选项setFocusable;我会做我需要的!

随机推荐

  1. bluetooth-lowenergy – Altbeacon库无法在Android 5.0上运行

    昨天我在Nexus4上获得了Android5.0的更新,并且altbeacon库停止了检测信标.似乎在监视和测距时,didEnterRegion和didRangeBeaconsInRegion都没有被调用.即使RadiusNetworks的Locate应用程序现在表现不同,一旦检测到信标的值,它们就不再得到更新,并且通常看起来好像信标超出了范围.我注意到的一点是,现在在logcat中出现以下行“B

  2. android – react-native动态更改响应者

    我正在使用react-native进行Android开发.我有一个视图,如果用户长按,我想显示一个可以拖动的动画视图.我可以使用PanResponder实现这一点,它工作正常.但我想要做的是当用户长按时,用户应该能够继续相同的触摸/按下并拖动新显示的Animated.View.如果您熟悉Google云端硬盘应用,则它具有类似的功能.当用户长按列表中的任何项目时,它会显示可拖动的项目.用户可以直接拖

  3. android – 是否有可能通过使用与最初使用的证书不同的证书对其进行签名来发布更新的应用程序

    是否可以通过使用与最初使用的证书不同的证书进行签名来发布Android应用程序的更新?我知道当我们尝试将这样的构建上传到市场时,它通常会给出错误消息.但有没有任何出路,比如将其标记为主要版本,指定市场中的某个地方?解决方法不,你不能这样做.证书是一种工具,可确保您是首次上传应用程序的人.所以总是备份密钥库!

  4. 如何检测Android中是否存在麦克风?

    ..所以我想在让用户访问语音输入功能之前检测麦克风是否存在.如何检测设备上是否有麦克风.谢谢.解决方法AndroidAPI参考:hasSystemFeature

  5. Android – 调用GONE然后VISIBLE使视图显示在错误的位置

    我有两个视图,A和B,视图A在视图B上方.当我以编程方式将视图A设置为GONE时,它将消失,并且它正下方的视图将转到视图A的位置.但是,当我再次将相同的视图设置为VISIBLE时,它会在视图B上显示.我不希望这样.我希望视图B回到原来的位置,这是我认为会发生的事情.我怎样才能做到这一点?编辑–代码}这里是XML:解决方法您可以尝试将两个视图放在RelativeLayout中并相对于彼此设置它们的位置.

  6. android – 获得一首歌的流派

    我如何阅读与歌曲相关的流派?我可以读这首歌,但是如何抓住这首歌的流派,它存放在哪里?解决方法检查此代码:

  7. android – 使用textShadow折叠工具栏

    我有一个折叠工具栏的问题,在展开状态我想在文本下面有一个模糊的阴影,我使用这段代码:用:我可以更改textColor,它可以工作,但阴影不起作用.我为阴影尝试了很多不同的值.是否可以为折叠文本投射阴影?

  8. android – 重用arm共享库

    我已经建立了armarm共享库.我有兴趣重用一个函数.我想调用该函数并获得返回值.有可能做这样的事吗?我没有任何头文件.我试过这个Android.mk,我把libtest.so放在/jni和/libs/armeabi,/lib/armeabi中.此时我的cpp文件编译,但现在是什么?我从objdump知道它的名字编辑:我试图用这个android.mk从hello-jni示例中添加prebuild库:它工作,但libtest.so相同的代码显示以下错误(启动时)libtest.so存在于libhello-j

  9. android – 为NumberPicker捕获键盘’Done’

    我有一个AlertDialog只有一些文本,一个NumberPicker,一个OK和一个取消.(我知道,这个对话框还没有做它应该保留暂停和恢复状态的事情.)我想在软键盘或其他IME上执行“完成”操作来关闭对话框,就像按下了“OK”一样,因为只有一个小部件可以编辑.看起来处理IME“Done”的最佳方法通常是在TextView上使用setonEditorActionListener.但我没有任何Te

  10. android – 想要在调用WebChromeClient#onCreateWindow时知道目标URL

    当我点击一个带有target=“_blank”属性的超链接时,会调用WebChromeClient#onCreateWindow,但我找不到新的窗口将打开的新方法?主页url是我唯一能知道的东西?我想根据目标网址更改应用行为.任何帮助表示赞赏,谢谢!

返回
顶部