有人能得到这个自定义AlertDialog实际工作? [英] Can someone get this custom AlertDialog to actually work?

查看:127
本文介绍了有人能得到这个自定义AlertDialog实际工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我问了一下自定义AlertDialog另一个问题<一href="http://stackoverflow.com/questions/7825720/extending-alertdialogs-in-android-where-to-find-examples-how-to-get-title-and">here.

I asked another question about custom AlertDialog here.

然后我点击我的方式,这个自定义AlertDialog(发现<一href="http://www.devdaily.com/java/jwarehouse/apps-for-android/Translate/src/com/beust/android/translate/AboutDialog.java.shtml"相对=nofollow>这里):

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:cedric@beust.com\">cedric@beust.com)", "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.

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

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

按照下面的建议,我修改了code,所以它看起来是这样的:

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);

    }
}

但我得到一个 BadTokenException 代替。

推荐答案

在创建altertdialog之后在重写的方法方法的OnCreate

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

 setContentView(R.layout.paus);

这将解决您的问题。

这篇关于有人能得到这个自定义AlertDialog实际工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆