我为TextWatcher
字段实施了EditText
,并在openContextMenu
中致电afterTextChanged
。以下是我的代码,它是onCreate
的一部分。
txtCorporateId.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count)
{
corpIdLoadedFromUrl = false;
setTextFieldStyles();
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after)
{
// do nothing
}
@Override
public void afterTextChanged(Editable s)
{
openContextMenu(txtCorporateId);
}
});
我还覆盖了清除和更新菜单项的onCreateContextMenu()
方法。但下面是我得到的例外
android.view.WindowManagerBadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:532)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:259)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:286)
at com.android.internal.view.menu.MenuDialogHelper.show(MenuDialogHelper.java:89)
at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:88)
at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2344)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
at android.view.View.showContextMenu(View.java:4508)
at android.app.Activity.openContextMenu(Activity.java:2933)
at com.ui.EnrollmentInstructionActivity$1.afterTextChanged(EnrollmentInstructionActivity.java:332)
at android.widget.TextView.sendAfterTextChanged(TextView.java:7424)
at android.widget.TextView.setText(TextView.java:3822)
at android.widget.TextView.setText(TextView.java:3671)
at android.widget.EditText.setText(EditText.java:80)
at android.widget.TextView.setText(TextView.java:3646)
at com.ui.EnrollmentInstructionActivity.loadEnrollmentDetailsFromSharedPreferences(EnrollmentInstructionActivity.java:864)
at com.ui.EnrollmentInstructionActivity.onCreate(EnrollmentInstructionActivity.java:574)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
任何人对此都有任何想法。我看到了这个例外的几个链接,但没有任何帮助我。