0
我在添加自动完成编辑字段时单击浮动操作按钮,当用户单击浮动操作按钮时,在弹出窗口中添加弹出窗口弹出窗口显示成功,但当我点击编辑字段时,它给出了一个错误。 错误是
这里是完整的错误。
Process: com.example.mubtadanaqvi.stunexus, PID: 7252
android.view.WindowManager$BadTokenException: Unable to add window -- token [email protected] is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:769)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:278)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.widget.PopupWindow.invokePopup(PopupWindow.java:1067)
at android.widget.PopupWindow.showAsDropDown(PopupWindow.java:966)
at android.widget.ListPopupWindow.show(ListPopupWindow.java:635)
at android.widget.AutoCompleteTextView.showDropDown(AutoCompleteTextView.java:1119)
at android.widget.AutoCompleteTextView.updateDropDownForFilter(AutoCompleteTextView.java:973) at android.widget.AutoCompleteTextView.onFilterComplete(AutoCompleteTextView.java:955)
at android.widget.Filter$ResultsHandler.handleMessage(Filter.java:285)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:548)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
这里是我的代码
floatingActionButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(LAYOUT_INFLATER_SERVICE);
final View customView = inflater.inflate(R.layout.add_new_skill, null);
mPopupWindow = new PopupWindow(
customView,
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
true
);
ImageButton cancel = (ImageButton) customView.findViewById(R.id.close_button);
cancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mPopupWindow.dismiss();
}
});
skillListAdapter = new ArrayAdapter<>(getContext(),android.R.layout.simple_dropdown_item_1line, skillsList);
skillInputField = (AutoCompleteTextView) customView.findViewById(R.id.skill_input);
skillInputField.setThreshold(1);
skillInputField.setAdapter(skillListAdapter);
skillListAdapter.addAll(skillsList);
skillListAdapter.notifyDataSetChanged();
Button saveButton = (Button) customView.findViewById(R.id.save_skill);
saveButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String skillText = skillInputField.getText().toString();
if (skillText.isEmpty())
makeToast("EMPTY");
else {
if (ConnectivityListener.isNetwork(getContext())) {
// progressDialog.show();
mPopupWindow.dismiss();
makeToast(skillText);
list.add(new Skill_Item(skillText, 0));
adapter.notifyDataSetChanged();
} else
makeToast("connection error!");
}
}
});
mPopupWindow.showAtLocation(rootLayout, Gravity.CENTER, 0, 0);
}
});
使用'this'代替'的getContext() ' –
@JohnJoe这将不起作用,因为ArrayAdapter是在setOnClickListner()中创建的,所以这不会返回Activity的引用。 –
使用'getActivity()'而不是'getContext()' – sodhankit