android - 长按 EditText 崩溃 : Unable to add window

标签 android android-layout android-view android-windowmanager android-window

我试图在 EditText 中长按,但是当我长按时,我收到以下错误。我希望能够长按以获取复制/粘贴/全选上下文弹出窗口,以便用户可以将文本粘贴到框中。

Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@799bfc6 is not valid; is your activity running?

EditText 在 PopupWindow 的 ScrollView 中。因此,当错误发生时,我当前在 PopupWindow 打开的 Activity 上处于 Activity 状态,我在 PopupWindow 中包含的 EditText 中进行了长按。

渐变设置

compileSdkVersion 25
buildToolsVersion '25.0.0'
defaultConfig {
    applicationId 'com.accoservice.cico'
    minSdkVersion 17
    targetSdkVersion 25
    versionCode 37
    versionName '4.2.6'
    multiDexEnabled true
}

包含 EditText 的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/outer_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#73000000">

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:layout_marginTop="100dp"
        android:layout_marginBottom="5dp"
        android:background="#ffffff"
        android:orientation="vertical">

        <TextView
            android:id="@+id/title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginLeft="5dp"
            android:layout_marginTop="15dp"
            android:singleLine="true"
            android:text="@string/note_msg"
            android:textColor="#62CCFE"
            android:textSize="18sp" />

        <View
            android:layout_width="fill_parent"
            android:layout_height="2dp"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="10dp"
            android:background="#62CCFE" />

        <ScrollView
            android:id="@+id/sv_resolution_note"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5dp"
            android:layout_marginRight="5dp"
            android:layout_marginTop="5dp">

            <EditText
                android:id="@+id/et_note_msz"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_above="@+id/view"
                android:layout_alignParentTop="true"
                android:scrollbars="vertical"
                android:focusable="true"
                android:gravity="left"
                android:maxLines="20"
                android:hint="@string/write_note"
                android:inputType="textFilter|textMultiLine|textCapSentences"
                android:singleLine="false"
                android:textIsSelectable="true"
                android:enabled="true"
                android:longClickable="true" />
        </ScrollView>

        <View
            android:id="@+id/view"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_above="@+id/send_note"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:background="@android:color/darker_gray" />

        <Button
            android:id="@+id/send_note"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@+id/viewss"
            android:layout_gravity="center"
            android:background="@color/white"
            android:text="@string/add_note" />

        <View
            android:id="@+id/viewss"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"

            android:background="@android:color/darker_gray" />

    </LinearLayout>

</LinearLayout>

弹出窗口:

@Override
public void onClick(View v) {
     noteDialog(getResources().getString(R.string.laborentryresolutionstart), tv_labor_entry_resolution_start);
}

public void noteDialog(String noteTitle, final TextView tv_resolution_note)
{
    LayoutInflater layoutInflater = (LayoutInflater) getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
            final View popupView;
            popupView = layoutInflater.inflate(R.layout.resolution_note, null);

            TextView title = (TextView) popupView.findViewById(R.id.title);
            title.setText(noteTitle);

            final EditText editText = (EditText) popupView.findViewById(R.id.et_note_msz);
            final PopupWindow popupWindow = new PopupWindow(popupView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT, true);
            popupWindow.update();
            popupWindow.setFocusable(true);
            popupWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            editText.setEnabled(false);
            editText.setEnabled(true);
            editText.setFocusable(true);
            editText.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    //ADD HERE ABOUT CUT COPY PASTE
                    // TODO Auto-generated method stub
                    return false;
                }
            });

            if (!tv_resolution_note.getText().toString().isEmpty()) {
                editText.setText(tv_resolution_note.getText().toString());
            }

            Button btnDone = (Button) popupView.findViewById(R.id.send_note);
            LinearLayout outer_layout = (LinearLayout) popupView.findViewById(R.id.outer_layout);
            outer_layout.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    popupWindow.dismiss();

                    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
                }
            });

            System.gc();
            try {
                btnDone.setOnClickListener(new Button.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        EditText noteMsz = (EditText) popupView.findViewById(R.id.et_note_msz);
                        tv_resolution_note.setText(noteMsz.getText().toString());

                        popupWindow.dismiss();

                        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                        imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);

                        invalidateOptionsMenu();
                    }
                });
            } catch (Exception e) {
            }

            popupWindow.setFocusable(true);
            popupWindow.setBackgroundDrawable(new BitmapDrawable(null, ""));
            popupWindow.showAsDropDown(tv_labor_sym_entry, 0, -60);
            popupWindow.update();     
}

最佳答案

据我所知,您收到此错误的原因是 outer_layoutonClickListeneronLongClickListener 一起被触发您的 editText。而且,由于 popupWindow.dismiss 是在 outer_layout 的点击监听器中调用的,因此弹出窗口会在 editText 的长按监听器之前被关闭代码可以运行,从而导致错误。

对此最简单的解决方案是为您的 onLongClick 方法返回 true :-

editText.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {

                    //ADD HERE ABOUT CUT COPY PASTE
                    // TODO Auto-generated method stub
                    return true;
                }
            });

这样做,您将消耗给定的长按,并且不会触发任何其他不需要的监听器。

onLongClick() - This returns a boolean to indicate whether you have consumed the event and it should not be carried further. That is, return true to indicate that you have handled the event and it should stop here; return false if you have not handled it and/or the event should continue to any other on-click listeners.

关于android - 长按 EditText 崩溃 : Unable to add window,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46121947/

相关文章:

android - 使用 onDraw 扩展 android 按钮

android - 是否可以有一个透明的 SYSTEM_ALERT_WINDOW 让 TouchEvents 通过?

java - Android Things I2C 驱动程序 PioException : I/O error

java - Horizo​​ntalScrollView 中的可点击图像

android - 如何在android中创建页面控件

android - 如何在 Android 中制作响应式布局在所有尺寸的移动设备和选项卡中都应该看起来不错,在横向纵向 View 中也应该看起来不错

android-view - 带有wrap_content的View的默认宽度和高度

android - 无法运行应用错误 :Execution failed for task ':app:transformClassesWithDexForDebug'

android - react native 自定义字体

java - 我可以在运行时更改 Android 应用程序样式吗?