有人能让这个自定义的 AlertDialog 真正起作用吗?
我问了关于自定义 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
重写 altertdialog 的 create 方法并在重写的 oncreate 方法中调用以下方法,
这将解决您的问题。
override on create method of altertdialog and call following methods in overridden method oncreate
this will solve your problem.
setContentView() 必须在 setTitle() 之前调用 ...
setContentView() must be called before setTitle() ...
为什么不在 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