0

I can't seem to be able to use the toast notification properly. In all of my other apps it worked great but in this one it doesn't. In this app I started using openGL with a framework from a book named "Beginning Android Games" and now I don't seem to be able to use the toast notification. I have no idea what to do... It fails because of the context. How can I make a context that will work? Please help me! this is part of my code because the code is too long:

private void updateReady() {
Coin.number = 0;
if (game.getInput().getTouchEvents().size() > 0) {
    state = GAME_RUNNING;
    Coin.number = 0;
    Num.number = 0;

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();
    }
}

When I put the line:

    Toast.makeText(this, "Start!", Toast.LENGTH_SHORT).show();

in the class that extends Activity and run it it just doesn't do anything... I tried to make it into a method and call it from other classes but it got a force close...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Baruch
  • 1,618
  • 5
  • 23
  • 42

4 Answers4

1

You can try using getApplicationContext() to get a reference to the current Activity context

Cody Caughlan
  • 32,456
  • 5
  • 63
  • 68
  • when i type it, it gives me an error sating "The method getApplicationContext() is undefined for the type GameScreen". probably because I am not extending Activity. I am extending GLScreen, if you worked with this framework before you might know what i'm talking about. Anyways.. how can i extend both ? – Baruch Nov 29 '11 at 02:26
  • @Baruch, try `getApplication()`. – st0le Nov 29 '11 at 04:07
0

Maybe this helps.

I had class defined like this

    public class tutorialThree extends Activity implements View.OnClickListener

I tried to use Toast like this

    Toast.makeText(this, "Wallpaper set", Toast.LENGTH_SHORT).show();

Did not work, because my class implements that interface "View.OnClickListener" (or whatever it is :)) So toast gets confused with "this" reference, you have to be more precise, so add name of your class before "this" keyword, like this:

    Toast.makeText(tutorialThree.this, "Wallpaper set", Toast.LENGTH_SHORT).show();

This solved my problem, now i can see toast.

Joakim
  • 1
0

I assume you're working with this framework called "Beginning Android Games 2".

According to this code, the instance variable you need here is glGame, which is a GLGame object. It extends Activity, so you can just do this:

Toast.makeText(glGame, "Start!", Toast.LENGTH_SHORT).show();
derfshaya
  • 338
  • 1
  • 9
0

You could always make your application context, or the context of whatever activity you're starting your GameScreen from, available statically or by passing it as an argument from whatever creates your instance.

Having said that, though, beware of memory leaks!

Amos M. Carpenter
  • 4,848
  • 4
  • 42
  • 72
  • But none of them extends Activity.. is it still possible? – Baruch Nov 29 '11 at 02:59
  • Well, surely somewhere along your chain (following it back from your code above to where the app is launched) you must have an activity, right? So just pass the context along to the class that needs it... Edit: Or, like I said, make it available statically... see [this question](http://stackoverflow.com/questions/2002288/static-way-to-get-context-on-android) as well. – Amos M. Carpenter Nov 29 '11 at 03:34
  • I looked at the question but didn't really understand the answer... Can you please write for me the right code? Btw i tried some of the answers but i got a force close... – Baruch Nov 29 '11 at 04:07
  • I can't write a solution for you without knowing your code... are you sure you had a look at what I said and tried to follow the chain back from where your game screen gets initialised to where you've got ahold of an activity (possibly your main activity)? You can just put a break point in your code and "Debug As... -> Android App" and see what calls what if you don't remember. That should give you an idea of how to get a handle on an activity. – Amos M. Carpenter Nov 29 '11 at 04:29
  • You're not making it easy for the people who are trying to help if you don't give us better information on what is going wrong. Perhaps you should consider starting with some easier tutorials, e.g. [vogella](http://www.vogella.de/android.html), rather than jumping into game development? I'm getting no indication that you're listening to or trying out what I've said. Good luck finding what you're after. – Amos M. Carpenter Nov 29 '11 at 04:46
  • The main thing that goes wrong is the context. I checked in the logcat and it says that when it calls the toast it has problem with the context. everything else is alright. I am sorry if it is hard to understand me. English is my second language and I am not an expert in android. – Baruch Nov 29 '11 at 04:56