Loading a drawable from a non-view class

All your problems with Audio, Video and Images.

Loading a drawable from a non-view class

Postby Cucho » Tue May 12, 2009 10:34 pm

Iam using MVC model for a game and i want to save a reference por a Drawable of each unit. The problem is that i cant initialize in the usual way

this.getContext.getResources().getDrawable(R.drawable.unit) => this wors only in a "view" class.

I want to initialize the Drawable from a class that extends nothing.

Thanks! Sorry for my english.
Cucho
Freshman
Freshman
 
Posts: 3
Joined: Tue May 12, 2009 10:25 pm

Top

Postby padde » Wed May 13, 2009 1:04 am

Just use a static reference of your acitivty context

sample
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class test extends Activity {
  3.  
  4.     public static Context myContext;
  5.  
  6.  
  7.  
  8.     public void onCreate(Bundle savedInstanceState) {
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         myContext = this;
  13.  
  14.         ...
  15.  
  16.     }    
  17.  
  18. }
  19.  
  20.  
  21.  
  22. public class extendsnothing {
  23.  
  24.     private Drawable mydrawable;
  25.  
  26.  
  27.  
  28.     public extendsnothing() {
  29.  
  30.         mydrawable = test.myContext.getResources().getDrawable(R.drawable.unit);
  31.  
  32.     }
  33.  
  34. }
  35.  
  36.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


or pass the context as a parameter if you dont like static stuff
padde
Master Developer
Master Developer
 
Posts: 443
Joined: Wed Apr 08, 2009 4:52 pm

Postby Cucho » Wed May 13, 2009 1:07 am

padde wrote:Just use a static reference of your acitivty context

sample
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public class test extends Activity {
  2.     public static Context myContext;
  3.  
  4.     public void onCreate(Bundle savedInstanceState) {
  5.         super.onCreate(savedInstanceState);
  6.         myContext = this;
  7.         ...
  8.     }    
  9. }
  10.  
  11. public class extendsnothing {
  12.     private Drawable mydrawable;
  13.  
  14.     public extendsnothing() {
  15.         mydrawable = test.myContext.getResources().getDrawable(R.drawable.unit);
  16.     }
  17. }
  18.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4


or pass the context as a parameter if you dont like static stuff


I dont like it but i was forced to use to implement MVC (thats the solution I found).

Thanks!
Cucho
Freshman
Freshman
 
Posts: 3
Joined: Tue May 12, 2009 10:25 pm

Top

Return to Multimedia Problems

Who is online

Users browsing this forum: No registered users and 8 guests