Android 长按编辑文本崩溃:无法添加窗口
我试图在Android 长按编辑文本崩溃:无法添加窗口,android,android-layout,android-view,android-windowmanager,android-window,Android,Android Layout,Android View,Android Windowmanager,Android Window,我试图在编辑文本中长时间单击,但当我长时间单击时,我收到以下错误。我想能够做一个长点击,以获得复制/粘贴/选择所有上下文弹出窗口,以便用户可以粘贴文本到框中 Fatal Exception: android.view.WindowManager$BadTokenException: Unable to add window -- token android.view.ViewRootImpl$W@799bfc6 is not valid; is your activity running? E
编辑文本中长时间单击,但当我长时间单击时,我收到以下错误。我想能够做一个长点击,以获得复制/粘贴/选择所有上下文弹出窗口,以便用户可以粘贴文本到框中
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的活动中处于活动状态,并在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
}
包含编辑文本的版面:
<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();
}
您可能太早调用弹出窗口。在我的示例()中,我通过按下按钮执行弹出代码。这是在onCreate()
和其他关键生命周期方法运行之后。有了这个例子,一切正常
但是,如果我尝试在onCreate()
中实例化弹出窗口,我会得到一个logcat错误,该错误表示:“android.view.WindowManager$BadTokenException:无法添加窗口”,这就是您看到的
我认为您试图过早地实例化弹出窗口。在活动生命周期的晚些时候启动它,并且一定要在执行onCreate()
之后启动。如果需要立即实例化,可以通过调用post(Runnable)
将代码附加到UI消息队列。(见附件)
我相当肯定这是你的问题。如果这没有帮助,请使用有关如何以及何时实例化弹出窗口的更多信息更新您的问题。根据我的说法,出现此错误的原因是外部布局的onClickListener
与编辑文本的onlongclickllistener
一起被触发。而且,由于在outer\u layout
的点击监听器内部调用了popupWindow.disease
,因此在运行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()-返回一个布尔值,指示您是否
已消耗该事件,不应继续进行。就是,
返回true表示您已经处理了事件,并且应该
到此为止;如果未处理它和/或事件,则返回false
应继续指向任何其他单击侦听器
在你的主要活动中
私有内容mContext
public void onCreate(){
mContext = this;
}
用mContext替换getBaseContext()
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
或
我们重新设计了弹出窗口,消除了原来的bug。您在活动中调用此代码吗?服务?你确定如果有活动,你没有被带离屏幕吗?该消息通常在您尝试从服务启动UI时出现,或者在活动完成后尝试弹出对话框时出现。我在活动中调用弹出窗口的代码。“活动”当前正在运行,弹出窗口处于活动状态,我在编辑文本中长按并收到错误。我在运行API 24的模拟器上运行了您的代码,但稍作修改,它对我来说运行正常。也就是说,它没有崩溃,长按侦听器按预期调用。你能提供一些关于你如何设置的更多信息吗?您正在测试的API?gradle设置:compileSdkVersion 25、minSdkVersion 17、targetSdkVersion 25看起来你正在做的事情和我正在做的完全一样。我的EditText在ScrollView中,如果有区别的话。我在问题中添加了更多的代码,以便您可以看到更多的情况。@Adam我无法用您提供的内容重现您的问题。如果你继续被这个问题所困扰,我建议你创建一个复制问题的模型。
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);