2014-11-02 33 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();