java - Android调用自定义alertdialog类
问题描述
这是我的自定义 dialogalertdialog 类。我想创建自定义对话框并调用每个活动,但出现了一些错误。我无法修复它。我不知道我该如何解决。
public class AlertForSelection extends AlertDialog {
private Context context;
private List<UpperCategory> lstUpper;
private int id;
private LinearLayout lnrList;
private String name;
public AlertForSelection(Context context, List<UpperCategory> lstUpper) {
super(context);
this.context = context;
this.lstUpper = lstUpper;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.alert_for_selection_view);
this.lnrList = (LinearLayout) findViewById(R.id.lnrList);
this.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
}
在这里,我通过单击按钮调用。
final AlertForSelection alertForSelection = new
AlertForSelection(getApplicationContext(), listUpperCategory);
alertForSelection.show();
这是错误
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
at android.view.ViewRootImpl.setView(ViewRootImpl.java:769)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:356)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:94)
at android.app.Dialog.show(Dialog.java:330)
at com.example.samcro.petshop.Activities.NewActivity.BtnUpCategory_Click(NewActivity.java:80)
at com.example.samcro.petshop.Activities.NewActivity.access$000(NewActivity.java:24)
at com.example.samcro.petshop.Activities.NewActivity$1.onClick(NewActivity.java:56)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24774)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6518)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
解决方案
你不能使用“getApplicationContext()”来新建一个 AlertDialog ,你应该将一个活动上下文附加到一个新的 AlertDialog 上,因为
Dialog(@NonNull Context context, @StyleRes int themeResId, boolean createContextThemeWrapper) {
// something not important
mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
final Window w = new PhoneWindow(mContext);
mWindow = w;
w.setCallback(this);
w.setOnWindowDismissedCallback(this);
w.setWindowManager(mWindowManager, null, null);
w.setGravity(Gravity.CENTER);
mListenersHandler = new ListenersHandler(this);
}
在这里,当你使用 show() 时,新的 Dialog() 函数将使用新 mWindowManager 的上下文
public void show() {
// something not important
mDecor = mWindow.getDecorView();
WindowManager.LayoutParams l = mWindow.getAttributes();
if ((l.softInputMode
& WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION) == 0) {
WindowManager.LayoutParams nl = new WindowManager.LayoutParams();
nl.copyFrom(l);
nl.softInputMode |=
WindowManager.LayoutParams.SOFT_INPUT_IS_FORWARD_NAVIGATION;
l = nl;
}
try {
mWindowManager.addView(mDecor, l);
mShowing = true;
sendShowMessage();
} finally {
}
}
这里 mWindowManager 需要 addView ,但是如果上下文是 applicationContext ,则 mWindowManager 的 token 将为空。如果上下文是activity,activity会将它的token绑定到mWindowManager,它可以运行良好。
推荐阅读
- c++ - C++ 方法声明不兼容
- r - How to create new column with an existing value conditional on another column in long format?
- powerbi - 我想在 powerBI 中创建一个度量,它是按月计算的不同计数的标准差
- kubernetes - 重新验证令牌 Kubernetes
- mailchimp - Mailchimp:从一位成员那里检索所有时事通讯/列表
- javascript - 防止 bxSlider 在滑动时链接
- python-3.x - 识别列表中的重复项
- android - TileService requestListeningState 在 Android Q 上不起作用(找不到 ComponentInfo 的磁贴)
- java - java比较器是类型感知的吗?
- php - 使用 PHP 和 keycloak 的 API REST 在他的领域创建用户