首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在片段中获取rootview?

如何在片段中获取rootview?
EN

Stack Overflow用户
提问于 2014-11-03 05:43:55
回答 2查看 21.6K关注 0票数 11

我正在使用这个库在我的应用程序上使用表情符号键盘。https://github.com/ankushsachdeva/emojicon

自述文件规定,必须使用activity layout hierarchy的最顶层视图初始化弹出窗口。

我的应用程序是通过片段实现的。

这是我用来测试的代码:

代码语言:javascript
复制
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;
}

如果我运行这段代码,我会在logcat中得到以下错误:

代码语言:javascript
复制
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?

编辑:我正在使用SherlockFragment

EN

回答 2

Stack Overflow用户

发布于 2014-11-03 05:50:10

将视图另存为fragment的实例成员,并在OnViewCreated方法中初始化Emojicons弹出窗口。这可能会解决你的问题。

代码语言:javascript
复制
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();
}

但是对于问题的标题,请查看here

票数 4
EN

Stack Overflow用户

发布于 2015-01-12 02:59:10

onCreateView调用后的Fragment或其他回调方法的onStart()中:

代码语言:javascript
复制
emojiconsPopup = new EmojiconsPopup(**getView()**, getActivity());
emojiconsPopup.setSizeForSoftKeyboard();

另一种选择是:

代码语言:javascript
复制
emojiconsPopup = new EmojiconsPopup(  getView().getRootView()  , getActivity());
emojiconsPopup.setSizeForSoftKeyboard();
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26704782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档