警告对话框与自定义布局失败 [英] Alert Dialog with custom layout failing
问题描述
因此,这涉及到一个问题,我刚才问。我想显示使用指定的布局警报。我的布局是:
So this is related to a question I asked earlier. I am trying to display an alert using a specified layout. My layout is:
<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>
而code调用并显示警告对话框是:
And the code to call and show the alert dialog is:
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();
当我运行它,我得到一个错误说:
When I run it I get an error saying:
未捕获的处理程序:螺纹主力退出,由于未捕获的异常
android.view.WindowManager $ NadTokenException:无法添加窗口 - 令牌null不是一个应用程序
Uncaught handler: thread main exiting due to uncaught exception android.view.WindowManager$NadTokenException: Unable to add window -- token null is not for an application
我已经通过Android开发网站看了看,不能弄明白。我想我只是失去了一些东西明显,但修复不是在我跳出。我怎样才能显示此警告对话框?
I've looked through the android development site and can't figure it out. I think I'm just missing something obvious but the fix isn't jumping out at me. How can I get this alert dialog to display?
推荐答案
不要使用 getApplicationContext()
。该方法是只在上下文可
(例如,活动
) - 使用上下文
您 AlertDialog.Builder
。
Do not use getApplicationContext()
. That method is only available on a Context
(e.g., Activity
) -- use that Context
for your AlertDialog.Builder
.
下面是从我的书一个示例项目,在其他中的事情,显示了 AlertDialog
基于掀起了自定义的查看
。
Here is a sample project from one of my books that, among other things, shows an AlertDialog
based off of a custom View
.
这篇关于警告对话框与自定义布局失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!