2014-11-02 32 views
4

我正在使用此库在我的应用程序上使用表情符号键盘。 https://github.com/ankushsachdeva/emojicon如何在碎片中获取rootview?

自述指出您的活动布局层次的最顶层视图必须使用初始化popupwindow。

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

这是我使用的测试代码:

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

+0

getActivity()getWindow()getDecorView()getRootView()。; – 2017-05-17 13:00:14

回答

3

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

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(); 
} 

但对于问题的标题 - 后onCreateView调用来检查here

+0

它与相同的错误不断崩溃。我也尝试使用view.getRootView() – tobias 2014-11-02 21:56:01

+0

你可以尝试从活动内部看它是否存在异常吗? – Heisenberg 2014-11-03 07:22:01

+0

如果我在活动的onCreate方法中添加代码,它也会崩溃。我究竟做错了什么? – tobias 2014-11-04 21:00:26

0

Fragment或其他回调的方法的onStart()

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

另一种方法是:

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