有人能让这个自定义的 AlertDialog 真正起作用吗?

发布于 2024-12-11 03:33:59 字数 3047 浏览 0 评论 0原文

我问了关于自定义 AlertDialog 这里

然后我点击了这个自定义的AlertDialog(找到此处):

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setContentView(R.layout.about_dialog);

        setTitle(R.string.about_title);
        setCancelable(true);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadData("Written by Cédric Beust (<a href=\"mailto:[email protected]\">[email protected])", "text/html", "utf-8");
    }

}

我这样修改:

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setTitle("Test");
        setCancelable(true);
        setContentView(R.layout.paus);
    }
}

然后尝试使用它,如下所示:

AboutDialog ad = new AboutDialog(getApplicationContext());
ad.show();

但我收到此错误:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at TestPackage.MainActivity$5.onClick(MainActivity.java:382)
    at android.view.View.performClick(View.java:2538)
  etc...

所以我想知道这是为什么正在发生。

=====================编辑============================0

作为根据下面的建议,我修改了代码,使其看起来像这样:

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paus);

    }
}

但我得到了一个 BadTokenException

I asked another question about custom AlertDialog here.

Then I clicked my way to this custom AlertDialog (found here):

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setContentView(R.layout.about_dialog);

        setTitle(R.string.about_title);
        setCancelable(true);

        WebView webView = (WebView) findViewById(R.id.webview);
        webView.loadData("Written by Cédric Beust (<a href=\"mailto:[email protected]\">[email protected])", "text/html", "utf-8");
    }

}

I modified like this:

import android.app.AlertDialog;
import android.content.Context;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
        setTitle("Test");
        setCancelable(true);
        setContentView(R.layout.paus);
    }
}

and then tried to use it, like this:

AboutDialog ad = new AboutDialog(getApplicationContext());
ad.show();

But I get this error:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
    at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:181)
    at com.android.internal.app.AlertController.installContent(AlertController.java:206)
    at android.app.AlertDialog.onCreate(AlertDialog.java:251)
    at android.app.Dialog.dispatchOnCreate(Dialog.java:307)
    at android.app.Dialog.show(Dialog.java:225)
    at TestPackage.MainActivity$5.onClick(MainActivity.java:382)
    at android.view.View.performClick(View.java:2538)
  etc...

So I'd like to know why this is happening.

===================== EDIT ==========================0

As per suggestions below, I modified the code so it looks like this:

import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.webkit.WebView;

/**
 * Display a simple about dialog.
 */
public class AboutDialog extends AlertDialog {

    protected AboutDialog(Context context) {
        super(context);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paus);

    }
}

But I get a BadTokenException instead.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

殤城〤 2024-12-18 03:33:59

重写 altertdialog 的 create 方法并在重写的 oncreate 方法中调用以下方法,

 setContentView(R.layout.paus);

这将解决您的问题。

override on create method of altertdialog and call following methods in overridden method oncreate

 setContentView(R.layout.paus);

this will solve your problem.

那小子欠揍 2024-12-18 03:33:59

setContentView() 必须在 setTitle() 之前调用 ...

setContentView() must be called before setTitle() ...

私野 2024-12-18 03:33:59

为什么不在 R.layout.param 中添加标题(文本视图),并且还可以添加按钮,然后所有内容都会显示

why don't you add a title (a text view) in R.layout.param and also you can add buttons and then everything will be shown

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文