How to get rootview in fragment?

I use this library to use emoji keyboard in my application. https://github.com/ankushsachdeva/emojicon

The Readme states that the topmost view of the activity layout hierarchy should be used to initialize popupwindow.

My application is implemented through fragments.

This is the code I use for testing:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.overview1_layout, container,
            false);

    // Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
    EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());

    //Will automatically set size according to the soft keyboard size        
    popup.setSizeForSoftKeyboard();

    popup.showAtBottom();


    return view;
}

If I run this code, I get the following error in logcat:

11-02 22:37:16.685: E/AndroidRuntime(30363): java.lang.RuntimeException: Unable to resume activity {com.Testing.full/com.Testing.full.MainActivity}: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?

EDIT: I am using SherlockFragment

+4
source share
2 answers

save the view as a member of the instance fragmentand initialize the Emojicons popup OnViewCreated. This may solve your problem.

public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.overview1_layout, container,
        false);

this.view = view

return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

// Give the topmost view of your activity layout hierarchy. This will be used to measure soft keyboard height
EmojiconsPopup popup = new EmojiconsPopup(view, getActivity());




//Will automatically set size according to the soft keyboard size        
popup.setSizeForSoftKeyboard();

popup.showAtBottom();
}

-

+3

onStart() Fragment onCreateView:

emojiconsPopup = new EmojiconsPopup(**getView()**, getActivity());
emojiconsPopup.setSizeForSoftKeyboard();

emojiconsPopup = new EmojiconsPopup(  getView().getRootView()  , getActivity());
emojiconsPopup.setSizeForSoftKeyboard();
0

All Articles