[英]Android calling custom alertdialog class
这是我自定义的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的令牌将为null。 如果上下文是活动,则活动会将其令牌绑定到mWindowManager,它将运行良好。
如果您将getApplicationContext()用作此类对话框的上下文
AlertForSelection alertForSelection = new AlertForSelection(getApplicationContext(), listUpperCategory);
然后使用YourActivityName.this
AlertForSelection alertForSelection = new AlertForSelection(YourActivityName.this, listUpperCategory);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.