2

我正在尝试创建一个包含相对布局的对话框,其中包含一个 web 视图。所以我创建了这个 showInterstitial 方法来显示对话框。但它每次都会抛出“无法添加窗口 - 令牌 null 不适用于应用程序”错误。

public void showInterstitial() {
    this.isInterstitial = true;
    openInterstitial(super.getContext(), showCloseButtonTime,
            autoCloseInterstitialTime, this);
}

private void openInterstitial(Context context, Integer showCloseButtonTime,
        Integer autoCloseInterstitialTime, InterstitialView adView) {

    if (showCloseButtonTime == null || showCloseButtonTime < 0)
        showCloseButtonTime = 0;
    if (autoCloseInterstitialTime == null || autoCloseInterstitialTime < 0)
        autoCloseInterstitialTime = 0;

    final Dialog dialog;

    dialog = new Dialog(context,
            android.R.style.Theme_NoTitleBar_Fullscreen);

    adView.dialog = dialog;

    if (adView.getParent() != null) {
        ((ViewGroup) adView.getParent()).removeAllViews();
    }

    final RelativeLayout mainLayout = new RelativeLayout(context);
    mainLayout.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    adView.setLayoutParams(new ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));
    mainLayout.addView(adView);

    Button closeButton = new Button(context);
    closeButton.setText("Close");
    RelativeLayout.LayoutParams closeParams = new RelativeLayout.LayoutParams(
            RelativeLayout.LayoutParams.WRAP_CONTENT,
            RelativeLayout.LayoutParams.WRAP_CONTENT);
    closeParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
            RelativeLayout.TRUE);
    closeParams.addRule(RelativeLayout.CENTER_HORIZONTAL,
            RelativeLayout.TRUE);
    closeButton.setLayoutParams(closeParams);
    closeButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });

    dialog.setContentView(mainLayout);
    dialog.show();
}
4

0 回答 0