应用程序在添加碎片后崩溃

时间:2015-11-04 13:14:27

标签: android android-fragments

我将这个https://github.com/rockerhieu/emojicon lib添加到我的应用程序中,现在这个lib要求我将片段视图添加到我的布局

<fragment
        android:id="@+id/emojicons"
        android:layout_width="match_parent"
        android:layout_height="220dp"
        class="com.rockerhieu.emojicon.EmojiconsFragment"/>

添加这个我的应用程序崩溃后,你能帮我解决这个问题吗? 这是logat

  

引起:android.view.InflateException:二进制XML文件行#90:   膨胀类片段错误

3 个答案:

答案 0 :(得分:0)

我一直在使用xml中的<fragment>标记时遇到问题。我建议切换到<FrameLayout>并在代码中使用以下方法:

FragmentManager manager = getSupportFragmentManager(); // or getFragmentManager() if you are not using android.support.v4.app.Fragment
manager.beginTransaction().replace(R.id.frame_layout, new EmojiconsFragment()).commit();

答案 1 :(得分:0)

您的活动必须实现此接口:

public class MainActivity extends Activity implements EmojiconGridFragment.OnEmojiconClickedListener, OnEmojiconBackspaceClickedListener{

并实施方法(在您的活动中):

    @Override
    public void onEmojiconClicked(Emojicon emojicon) {

    }

    @Override
    public void onEmojiconBackspaceClicked(View v) {

    }

为什么这不是用库自述文件写的,这很奇怪

答案 2 :(得分:0)

您应该将此用于编辑文本

<github.ankushsachdeva.emojicon.EmojiconEditText
        android:id="@+id/emojicon_edit_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_weight="8"
        emojicon:emojiconSize="28sp" />

然后在初始化emojitext后的类文件中,你将不得不使用这些监听器

    final EmojiconsPopup popup = new EmojiconsPopup(rootView, this);
        //Will automatically set size according to the soft keyboard size
        popup.setSizeForSoftKeyboard();

        /*--------------------------------------------------------------------*/

        popup.setOnDismissListener(new PopupWindow.OnDismissListener() {

            @Override
            public void onDismiss() {
                changeEmojiKeyboardIcon(emojiButton, R.mipmap.smiley);
            }
        });

//If the text keyboard closes, also dismiss the emoji popup
        popup.setOnSoftKeyboardOpenCloseListener(new EmojiconsPopup.OnSoftKeyboardOpenCloseListener() {

            @Override
            public void onKeyboardOpen(int keyBoardHeight) {

            }

            @Override
            public void onKeyboardClose() {
                if (popup.isShowing())
                    popup.dismiss();
            }
        });

       /*On emoji clicked, add it to edittext*/
        popup.setOnEmojiconClickedListener(new EmojiconGridView.OnEmojiconClickedListener() {

            @Override
            public void onEmojiconClicked(Emojicon emojicon) {
                if (emojiconEditText == null || emojicon == null) {
                    return;
                }

                int start = emojiconEditText.getSelectionStart();
                int end = emojiconEditText.getSelectionEnd();
                if (start < 0) {
                    emojiconEditText.append(emojicon.getEmoji());
                } else {
                    emojiconEditText.getText().replace(Math.min(start, end),
                            Math.max(start, end), emojicon.getEmoji(), 0,
                            emojicon.getEmoji().length());
                }
            }
        });

        //On backspace clicked, emulate the KEYCODE_DEL key event
        popup.setOnEmojiconBackspaceClickedListener(new EmojiconsPopup.OnEmojiconBackspaceClickedListener() {

            @Override
            public void onEmojiconBackspaceClicked(View v) {
                KeyEvent event = new KeyEvent(
                        0, 0, 0, KeyEvent.KEYCODE_DEL, 0, 0, 0, 0, KeyEvent.KEYCODE_ENDCALL);
                emojiconEditText.dispatchKeyEvent(event);
            }
        });


        // To toggle between text keyboard and emoji keyboard keyboard(Popup)
        emojiButton.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                //If popup is not showing => emoji keyboard is not visible, we need to show it
                if (!popup.isShowing()) {

                    //If keyboard is visible, simply show the emoji popup
                    if (popup.isKeyBoardOpen()) {
                        popup.showAtBottom();
                        changeEmojiKeyboardIcon(emojiButton, R.mipmap.ic_action_keyboard);
                    }

                    //else, open the text keyboard first and immediately after that show the emoji popup
                    else {
                        emojiconEditText.setFocusableInTouchMode(true);
                        emojiconEditText.requestFocus();
                        popup.showAtBottomPending();
                        final InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        inputMethodManager.showSoftInput(emojiconEditText, InputMethodManager.SHOW_IMPLICIT);
                        changeEmojiKeyboardIcon(emojiButton, R.mipmap.ic_action_keyboard);
                    }
                }

                //If popup is showing, simply dismiss it to show the undelying text keyboard
                else {
                    popup.dismiss();
                }
            }
        });

希望这会对你有所帮助:)。