Android调用自定义Alertdialog类

时间:2018-10-10 11:05:11

标签: 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)

2 个答案:

答案 0 :(得分:4)

您不能使用“ 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,它将运行良好。

答案 1 :(得分:3)

如果您将getApplicationContext()用作此类对话框的上下文

AlertForSelection alertForSelection = new AlertForSelection(getApplicationContext(), listUpperCategory);

然后使用YourActivityName.this

AlertForSelection alertForSelection = new AlertForSelection(YourActivityName.this, listUpperCategory);