aspose file tools*
The moose likes Android and the fly likes sharing variables between classes Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Mobile » Android
Bookmark "sharing variables between classes" Watch "sharing variables between classes" New topic
Author

sharing variables between classes

Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
Hi All
Well I'm a noob starting to learn to program in java and android and am trying to make a live wallpaper.
This seems like a stupid question but I am a noob so please be patient.
I want to scale all images and potions by one scale number so all images fit the screen properly (using scaleBitmap) and are postioned properly no matter what the screen size is.

The code I have in the onSurfaceChanged in the main java file is this.



So the background image is 1024x1024 which is the biggest image so all measurements gets scaled down form this or fit the same size

gives me the number I need to use for scaling everything, now the big problem
How do I get this number into other class files to use it?
I tried a few methods and have some samples of something similar but not the same and tried reading up on it in learn java for android from java jeff but I'm not getting it yet.

After 4 hours of trying different things I decided to ask for some help so I'm asking and its probably simple for someone experienced but as I said I'm a noob.

Thanks for any help anyone can provide me in this.

Sam
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
One approach would be to have a Java class that has methods like "public static void setScale(int)" and "public static int getScale()". That works in non-Android contexts as well, but is somewhat unelegant.

A more Android-specific way would be for the app to use a custom android.app.Application class; see this for details.


Ping & DNS - my free Android networking tools app
Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
Wow fantastic
Thank you very much for this.
I will try it tonight when I get home, if it works and I don't mess it up somehow I'll post the code for others so they can learn also.
You been by far the biggest help yet.
Again I thank you very much.
Nice apps by the way
Sam
Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
Well I tried a few more things and read some more things still no go

I'll add the code and show you what I tried Ulf, maybe you have an answer it is probably just my inexperience at jave.

In the main file called SkyMountain I have this (just adding the code that needed for this.



Now I try and bring this scaler number into the Mountain file so I can scaled the background image



It won't work, frustrating this language is, I used to program in flash, php, c++ c and nothing seems harder for me than java for some reason, all the rest I did learn and got it pretty quickly and even became a tech reviewer for flash books within a year, mind you that was years ago and I pretty much forgotten everything because I haven't coded for about 7 or 8 year now, but it seems java is the hardest of the lot, hopefully I'll get it sooner than later and I will keep plugging away at it till I do.

If you can see the errors of my ways here in this code please show me them if you wouldn't mind or anyone else.

Thanks again for any help anyone can provide in advance.

Sam
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
While you didn't say *how* it doesn't work, this code doesn't implement either of the approaches I mentioned. The SkyMountain class doesn't extend Application, so it's not the Android-specific way I mentioned. And neither the 'samsScaler' int nor its two accessor methods are static, so it's not the other approach, either. Also, I don't see setSamsScaler being called - before you do that, the value will be zero.
Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
Thanks Ulf

I tried something else I read also and bad results, I didn't add a log it just wouldn't load, I did more reading today and tried the code you suggested but still am getting bad results, the main problem is I'm trying to create a live wallpaper as a project to learn from, while reading Hello Android book and Java Jeffs book learn java for android all at the same time, an extremely frustrating way to learn this stuff but it worked for me in the past as it forces me to problem solve and somehow I usually learn more and quicker this way (not including all the bumps in the head from banging my head against a wall from frustration lol) not a method I would ever recommend to anyone but it works for me in the long run. (Even though its frustrating as heck.
Anyhow I tried to extend Applicaton and write the code then I got local variable is never read it suggested I create a set and get so I pressed ok, all the warning left, so heres this part of that code. that seems to be ok so far.



Next I want to use this in the mountain.java class to read the new int so I can scaled the background image but again the local variable cannot be read which is where the issue is and hopefully I can figure it out but heres the code.


This part of the code is where the local variable is not being read

The variable is theScaler I will try a few other things because I really like this method of bring it over, just had to wipe out the manifest first add it then add the manifest again to read it all like it stated in that tutorial. So I will keep reading and testing and trying, if you got an answer or anyone else (probably something simple as I'm totally new to android and java (like making the variables static, can't believe I forgot that one), been a long time since I coded I guess thats obvious.

Anyhow its late I give up for tonight.
I keep plugging away and reading more tomorrow.

Thanks in advance again and again for anyhow help and knowledge you can pass along, you been a great help and I thank you.
Sam
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
TheScaler theScaler = (TheScaler)getApplication();
scaled = samsScaler;

This looks odd - why pass around an integer; isn't that what you intended the helper classes to handle? And "theScaler" isn't used, so that statement is superfluous.

It's also confusing to have the variable and its accessor methods in both the SkyMountainEngine class and the TheScaler class; you may want to delete one of them, to make sure you're not inadvertently calling the wrong one. Plus, I don't see getSamsScaler (or getSamScaler) being called anywhere.

Lastly, is the Application class set up correctly, including the entry in the AndroidManifest.xml file? From where it is defined -buried in another class that isn't public- I doubt that it is.
Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
TheScaler theScaler = (TheScaler)getApplication();
scaled = samsScaler;

be

instead?

The reason I didn't set up a seperate class is because I'm trying to do it within this one where the variable I need is at.
I was wondering that and even checked to see if I could add 2 extends in one class which I found out you couldn't according to Java Jeffs books.

I called the SkyMountain class in the manifest file but not the other class because its nested inside that class.
I'll keep fooling around with it till I get it and understand it fully.
I'll keep reading both books for this also, hopefully I'll get it sooner than later

Thanks again.
Sam
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41508
    
  53
... be ... instead?

I don't see what difference that would make.

I think you're confusing yourself by having remnants of 3 different approaches to this in your code. I'd delete them all and start over. Same with having multiple variables with the same or similar names; that's bound to lead to confusion and errors.

From your comment it's clear that you're not using the Application-class approach, so you can safely remove that. I still think it'd be a better approach, though.
Sam Riggs
Greenhorn

Joined: Mar 20, 2011
Posts: 8
Wow I'm at work and just looked at it lol , your right it is confusing as heck, I was tired when I tried that one and I noticed samsScaler doesn't do a thing.
Oh well back to the drawing board, I guess I'll have to stick with static one this one.
If I get the thing working I'll post it for others.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: sharing variables between classes