Q: Thread a Dread

Put your problem here if it does not fit any of the other categories.

Q: Thread a Dread

Postby doubleslash » Sun Mar 01, 2009 6:52 pm

Hi,
How can I change a public static variable in Activity from a secondary thread? In my Activity class, I have
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class Myapp extends Activty{
  2.  
  3. public static MyObject count;
  4.  
  5. public MyObject get(){
  6.  
  7. return count;
  8.  
  9. }
  10.  
  11. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
Inside a task which is run on a secondary thread, I'd like to set the variable count as follows
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyView extends SurfaceView implements SurfaceView.Callback{
  2.  
  3. //inside task
  4.  
  5. public void run(){
  6.  
  7. Myapp myapp = new Myapp();
  8.  
  9. myapp.get().set(1); //problem here
  10.  
  11. }
  12.  
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
MyObject is a simple custom defined class which supports get() and set() methods. The class definition is given as
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class MyObject{
  2.  
  3. private int c;
  4.  
  5. public MyObject(){
  6.  
  7. c = 0
  8.  
  9. }
  10.  
  11. public void set(int count){
  12.  
  13. c = count;
  14.  
  15. }
  16.  
  17. public int get(){
  18.  
  19. return c;
  20.  
  21. }
  22.  
  23. }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
What's wrong? Is it because I call set from a seondary thread?
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Postby MrSnowflake » Mon Mar 02, 2009 9:48 am

You can't change static variables from non static functions, that's basic java.

Might I suggest, as you are asking a couple of such newbie questions (which isn't a real problem, though you can easilly solve these yourself), to learn some more java first, go through the Java tutorials on the java site. After that, your development will ge a lot more quick.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: Google [Bot] and 11 guests