【问题标题】:Android AlertDialog and PreferenceActivityAndroid AlertDialog 和 PreferenceActivity
【发布时间】:2023-03-04 20:40:01
【问题描述】:

我创建了一个带有自定义标题的 PreferenceActivity。在标题中我添加了一个按钮。在我添加的按钮点击事件中:

imageClearCache.setOnClickListener(new OnClickListener() {
              public void onClick(View v) { 
....


    new AlertDialog.Builder(getApplicationContext())
                      .setTitle("Alert!!")
                      .setMessage("Text...")
                      .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int which) { 
                                // continue with delete
                            }
                         }).show();
    }

我收到错误:...无法添加窗口 -- 令牌 null 不适用于应用程序

有什么想法吗?

最好的问候

【问题讨论】:

    标签: android android-alertdialog preferences


    【解决方案1】:

    使用被点击的视图提供的正常上下文(在这种情况下是您的活动)而不是应用程序上下文。

    new AlertDialog.Builder(v.getContext())
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多