0

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?

Community
  • 1
  • 1
Max Mumford
  • 2,482
  • 5
  • 28
  • 40

3 Answers3

3

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.

Yahel
  • 8,522
  • 2
  • 24
  • 32
  • I am doing this at the moment, there is a bug in AlertBuilder.dialog constructer where you cannot pass in getApplicationContext(); the only thing that works is passing in "this" from the activity. Passing this, however, still results in the "invalid token" exception mentioned in the thread I linked to in my post... – Max Mumford Feb 05 '12 at 11:18
  • From what I understand from your post, you are not at all doing what I'm telling you to try : Either use an Handler or a pass the activity context to your class where you store it as a local variable to be used specifically in the alertDialog call. Beside if my understanding is correct, applicationContext not working is not a bug, since the applicationcontext is not link to the ui thread. I'm going to update my answer with some code. – Yahel Feb 06 '12 at 09:33
  • Ok I see, so you are saying that the best way to show the dialogs is from within the activity class. The reason I wanted to have all the dialog code within the view itself was because wherever the view is deployed the dialog handling code will be the same - there is also a lot of dialog code that will have to be re-written everytime. Perhaps there is an easier way to handle the dialogs from within the activity that I havnt considered? – Max Mumford Feb 06 '12 at 14:41
  • If your object will be reused in more than one activity, then it would make sense not to use handler and to go with the second option I provided. Pass the calling activty to the object as a parameter on creation. I'll update my answer with some code. – Yahel Feb 06 '12 at 20:36
1

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

Wesley Wiser
  • 9,491
  • 4
  • 50
  • 69
0

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

getParent();

instead of this.And please show some code to us

Shahzad Imam
  • 2,030
  • 2
  • 26
  • 45
  • getParent within the view returns a ViewParent object which is not valid for dialog showing. I have also tried passing the activity into the class through the constructor and using getParent on that which did not work either... – Max Mumford Feb 06 '12 at 14:43