gpt4 book ai didi

android - 自定义布局失败的警报对话框

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:01:13 25 4
gpt4 key购买 nike

所以这和我之前问的一个问题有关。我正在尝试使用指定的布局显示警报。我的布局是:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp">
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
</LinearLayout>

调用和显示警告对话框的代码是:

    Context mContext = getApplicationContext();

AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
// use a custom View defined in xml
View view = LayoutInflater.from(mContext).inflate(R.layout.sell_dialog, (ViewGroup) findViewById(R.id.layout_root));
builder.setView(view);
builder.setPositiveButton(android.R.string.ok, new OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {

// do whatever you want with the input
}
});
AlertDialog alertDialog = builder.create();

alertDialog.show();

当我运行它时,我收到一条错误消息:

未捕获的处理程序:主线程由于未捕获的异常而退出android.view.WindowManager$NadTokenException: 无法添加窗口 -- token null 不适用于应用程序

我浏览了 android 开发站点,但无法弄明白。我想我只是遗漏了一些明显的东西,但修复并没有突然出现在我面前。如何显示此警报对话框?

最佳答案

不要使用 getApplicationContext()。该方法仅适用于 Context(例如,Activity)——将那个 Context 用于您的 AlertDialog.Builder.

Here is a sample project来 self 的一本书,其中显示了一个基于自定义 ViewAlertDialog

关于android - 自定义布局失败的警报对话框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2748414/

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