Newb Q: Activity Constructor??

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Newb Q: Activity Constructor??

Postby doubleslash » Sun Mar 01, 2009 1:57 am

Hi
I have a task to be run on a secondary thread defined in the class Activity
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Myapp extends Activity{
  2.  
  3. private count;
  4.  
  5. public Myapp(){}
  6.  
  7. public void task(){
  8.  
  9. //process count
  10.  
  11. }
  12.  
  13. }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
It is optimal to place the method task inside Myapp class because it utilizes a lot of the private variables defined there. The problem is I need to call it from another class extending View. As shown above, I created a Myapp constructor just so I can call task from MyView as shown below
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public void MyView extends SurfaceView implements SurfaceView.Callback{
  2.  
  3. public void surfaceCreated(){
  4.  
  5. new Myapp().task();
  6.  
  7. }
  8.  
  9. }
Parsed in 0.030 seconds, using GeSHi 1.0.8.4
This program doesn't work, and I don't know if it's because of my creating a constructor inside Activity. I've never seen this done anywhere, so I'm uncertain about it. Is this ok and the bug is something else?
Thanks for looking
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

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

In java, you can define fields (and members) as package (which means, leaving out private). This means, other classes in the same package have full access to fields (and members) of with the package modifier. That way, you can call hidden fields from your thread while it's in another class than the Activity itself.
User avatar
MrSnowflake
Moderator
Moderator
 
Posts: 1439
Joined: Sat Feb 16, 2008 3:11 pm
Location: Flanders, Belgium

Postby doubleslash » Tue Mar 03, 2009 4:23 am

That solved my problem. I have to crank out a project as quickly as possible, but when I'm done, I'll definitely study a java book cover to cover. Thanks so much.
doubleslash
Developer
Developer
 
Posts: 30
Joined: Fri Feb 13, 2009 6:05 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 8 guests