Error in Spinner in a Popup Window, it Crashes when Clicked
Asked Answered
J

2

6

I have a PopupWindow that is called from a Fragment and everything works except my Spinner. When I click in my Spinner it crashes and gives the error:

"android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@421a46d8 is not valid; is your activity running?"

My Code to call the popup window in the fragment:

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

My Code in ManagerPopUp to show 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 , 0);
            return popUp;
        }
}

My Spinner Adapter:

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, 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, 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;
    }
}

The View how it Appears in my App:

enter image description here

And the error that I get when I click in My Spinner (Red Label in the image):

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)
Jory answered 19/9, 2014 at 11:10 Comment(3)
May be your mContext=null check it up...Swank
My context is not Null, i don't know if I get the right context, but is not null. The popup opens and uses the Context. Is the Spinner in the popup window that crashes the app if we click on it.Jory
very well explained. estou tendo o mesmo problema.Efficiency
B
13
final PopupWindow popUp = new PopupWindow(mContext);

check your context there. Is it null?

Edit: add this to your spinner if you show spinner in dialog

android:spinnerMode="dialog"

If you create spinner dynamically:

Spinner spinner = new Spinner(context, Spinner.MODE_DIALOG);
Brazilin answered 19/9, 2014 at 11:14 Comment(3)
My context is not Null, i don't know if I get the right context, but is not null. The popup opens and uses the Context. Is the Spinner in the popup window that crashes the app if we click on it.Jory
It worked, but not the way I intended. I was hoping that appears like the dropdown mode. But the Dialog mode is working. I think the dropdown mode does not work in PopupWindow it crashes the app. Thanks for the help.Jory
@Bernas It is not working for me in poup window. Can you please help to do it? I'm running it on android device api 25.Dreamworld
O
3

Pass Activity.this Instead mContext in PopupWindow constructor.

Like:

If Activity:

PopupWindow popUp = new PopupWindow(ActivityName.this);

If Fragment:

PopupWindow popUp = new PopupWindow(getActivity());

Edited:

Now change Constructor with (Activity mcontext)

Activity mContext;

public ManagerPopUp(Activity mContext){
        this.mContext = mContext;
}
Obumbrate answered 19/9, 2014 at 11:35 Comment(4)
I already I am using "getActivity()", I pass the "getActivity()" to the variable mContext. See my first block of code, when I create my ManagerPopup Object in my Fragment.Jory
Where you passing getActivity(), How that reference you use here? i did not get you.Obumbrate
In my ManagerPopup object, when I created it in the fragment I pass the getActivity() by the constructor to my "mContext" variable. ManagerPopUp mManagerPopUp = new ManagerPopUp(getActivity());Jory
Change it the Context for Activity, and still crashes in mode dropdown. I think this mode does not work in a PopupWindow. I will use dialog mode. Thank you anyway.Jory

© 2022 - 2024 — McMap. All rights reserved.