Android:显示类中的对话框

发布于 2025-01-02 01:47:51 字数 417 浏览 1 评论 0原文

我编写了一个类来扩展 View 类以显示一些数据。用户需要能够通过单击数据并在对话框中显示各种选项来操作这些数据。

然而,我遇到的问题是,在 Android 中,要初始化 AlertDialog.Builder 实例,您必须传入“this”(如在活动中)才能使其工作(getApplicationContext() 不起作用 - 请参阅此文章: 对话框抛出“无法添加窗口 - 令牌 null 不适用于应用程序”,以 getApplication() 作为上下文

如何解决此问题并从扩展 View 的类中显示对话框?

I have written a class that extends the View class to show some data. The user needs to be able to manipulate this data by clicking on it and being presented with various options in a dialog box.

The problem I am getting, however, is that in Android, to initialise an AlertDialog.Builder instance, you must pass in "this" (As in an activity) in order for it to work (getApplicationContext() does not work - see this article: Dialog throwing "Unable to add window — token null is not for an application” with getApplication() as context)

How can I get past this problem and show the dialog from within my class that extends View?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

呆头 2025-01-09 01:47:51

如果您需要从非视图类打开消息框,那么您有两种解决方案:

或者

  • 从你的 android 视图来看,当你实例化你的类时,将上下文传递给你的类的构造函数,然后存储这个上下文。然后您就可以在警报框中使用它。

小心上下文传递机制的内存泄漏

编辑:

当我抓住机会去查看文档时,我正在编写一些代码来帮助您了解如何使用处理程序。通常那里没有任何用处,但对于这种特殊情况,哦奇迹,看看我发现了什么,完整且易于理解的代码示例,说明如何使用处理程序及其消息机制。它隐藏在可折叠标题下(“http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar”):http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar

评论后编辑2

因为操作希望其对象可以在不同的活动中重用,所以不使用处理程序而是将上下文(真正到调用活动的链接)传递给对象是有意义的。然后该对象将能够在dialog.builder 中使用此上下文。

在名为 MyActivity 的活动类的 oncreate 中:

MyCustomObject myObject = new MyCustomObject(this);

在对象类中,

Class MyCustomObject {
  private MyActivity mContext;

  void MyCustomObject(MyActivity context) {
    this.mContext = context;
  }

  private showDialog(String message) {
    AlertDialog.Builder alert = new AlertDialog.Builder(mContext);//we use the context
  }
}

当您完成对象时,不要忘记销毁并清空对话框构建器和 mContext。这可能会很快泄漏内存。

If you need to open a messagebox from a non view class then you have two solutions :

Or

  • From your android view, when you instantiate your class, pass a context to the constructor of your class and then store this context. You'll then be able to use it in your alertboxes.

Be careful of memory leaks with the context passing mecanism

EDIT :

I was writing some code to get you going on how to use handlers when I took a chance and went for the doc. Usually there is nothing of use there but for this particular case, oh miracle, look what I found, complete and easy to understand code example of how to use a handler and its message mecanism. It is hidden under the foldable title ("http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar") : http://developer.android.com/guide/topics/ui/dialogs.html#ShowingAProgressBar

EDIT2 AFTER COMMENTS

Because the op wants its object to be reusable in different activities it makes sense not to use handlers but instead to pass the context( a link to the calling activity really) to the object. The object will then be able to use this context in the dialog.builder.

In the oncreate of your activity class called MyActivity :

MyCustomObject myObject = new MyCustomObject(this);

In your object class

Class MyCustomObject {
  private MyActivity mContext;

  void MyCustomObject(MyActivity context) {
    this.mContext = context;
  }

  private showDialog(String message) {
    AlertDialog.Builder alert = new AlertDialog.Builder(mContext);//we use the context
  }
}

DO NOT FORGET TO DESTROY AND NULLIFY THE DIALOG BUILDER AND THE mContext when you are done with your object. This could leak memory really fast.

青巷忧颜 2025-01-09 01:47:51

使用 View.getContext() 并传递它到您的 AlertDialog.Build 实例中。

Use View.getContext() and pass that into your AlertDialog.Build instance.

屌丝范 2025-01-09 01:47:51

您必须发布一些代码,或者尝试用它

getParent();

代替这个。请向我们展示一些代码

You must have posted some code,Or lt try it with

getParent();

instead of this.And please show some code to us

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文