【问题标题】:How to get rootview in fragment?如何在片段中获取rootview?
【发布时间】:2014-12-29 13:13:16
【问题描述】:

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

自述文件指出,必须使用活动布局层次结构的最顶层视图来初始化弹出窗口。

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

这是我用于测试的代码:

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 中收到以下错误:

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

【问题讨论】:

  • getActivity().getWindow().getDecorView().getRootView();

标签: android view fragment


【解决方案1】:

将视图保存为fragment 的实例成员,并在OnViewCreated 方法中初始化Emojicons Popup。这可能会解决您的问题。

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

【讨论】:

  • 它不断崩溃并出现同样的错误。我也尝试使用 view.getRootView()
  • 您可以尝试在活动中执行此操作以查看异常是否仍然存在?
  • 如果我在活动的 onCreate 方法中添加代码,它也会崩溃。我做错了什么?
【解决方案2】:

FragmentonStart()onCreateView 调用之后的其他回调方法中:

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

另一种选择是:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2020-11-15
    • 1970-01-01
    • 2018-11-30
    • 1970-01-01
    • 2015-08-30
    • 1970-01-01
    相关资源
    最近更新 更多