gpt4 book ai didi

android - AlertDialog 无法显示自定义布局

转载 作者:太空宇宙 更新时间:2023-11-03 13:18:58 29 4
gpt4 key购买 nike

我使用以下代码 fragment 来显示自定义对话框:

btnCancel.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
// 1. Instantiate an AlertDialog.Builder with its constructor
AlertDialog.Builder builder = new AlertDialog.Builder(context);

// 2. Chain together various setter methods to set the dialog
// characteristics
builder.setTitle("Question");

AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{

public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
CallMethod();
}

}).setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}).create();
dialog.setContentView(R.layout.question);
dialog.show();
}
});

当我点击按钮时,我得到这个异常:

android.util.AndroidRuntimeException: requestFeature() must be called before adding content
at com.android.internal.policy.impl.PhoneWindow.requestFeature(PhoneWindow.java:229)
at com.android.internal.app.AlertController.installContent(AlertController.java:234)
at android.app.AlertDialog.onCreate(AlertDialog.java:337)
at android.app.Dialog.dispatchOnCreate(Dialog.java:355)
at android.app.Dialog.show(Dialog.java:260)
at com.example.MyApp.SimpleActivity$2.onClick(SimpleActivity.java:108)
at android.view.View.performClick(View.java:4207)
at android.view.View$PerformClick.run(View.java:17372)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

它需要什么样的请求?

最佳答案

您需要调用 setView 而不是 setContentView:

dialog.setView(R.layout.question);

并在创建对话框之前设置 View :

dialog.setView(R.layout.question).create();

[编辑]

 AlertDialog dialog= builder.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{

public void onClick(DialogInterface dialog, int whichButton)
{
dialog.dismiss();
CallMethod();
}

}).setNegativeButton("No", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
dialog.dismiss();
}
}).setView(R.layout.question).create();

关于android - AlertDialog 无法显示自定义布局,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31588904/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com