How to call getResources () from a class that has no context?

In my application, I have many classes and activities. A droid is a class that has no context. Mygame is a class that extends SurfaceView and implements SurfaceHolder.Callback. I create a Droid object in the mygame class and set the background image and position for it. The code I wrote for this is below.

block1 = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.birdpic), 100, 10); 

The constructor of the Droid class is given below.

 public Droid(Bitmap bitmap, int x, int y) { this.bitmap = bitmap; this.x = x; this.y = y; } 

In a specific scenario, I have to set the background image and the position of the Droid object from the Droid class itself. So I ran into a problem. Below is a code snippet for this.

 if(checkflag) { myObj.centerblock=new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.blast), myObj.xpos, myObj.ypos); } 

The problem is that the Droid class has no context. Therefore, I cannot use getResources () here. I tried the code below, but it crashes.

 if(checkflag) { myObj.centerblock=new Droid(BitmapFactory.decodeResource(myObj.getResources(), R.drawable.blast), myObj.xpos, myObj.ypos); } 

Can someone help me. I just want to set a background image and place it for a Droid object from the Droid class itself.

+48
android android-context class background
Nov 23 '11 at 7:30
source share
5 answers

A Context is a system handle; it provides services such as resource resolution, access to databases and preferences, etc. This is the โ€œinterfaceโ€ that allows you to access specific application resources and the class and information about the application environment. Your actions and services also expand the context to inherit all of these methods for accessing information about the environment in which the application runs.

This means that you need to pass the context to a specific class if you want to get / change some specific resource information. You can pass the context in the constructor, for example

 public classname(Context context, String s1) { ... } 
+45
Nov 23 '11 at 8:14
source share

The usual solution for this is to pass the instance of the context to the class as it is created or after it is created, but before you need to use the context.

Another solution is to create an application object with a static method to access the application context, although it pretty closely connects the Droid object with the code.

Edit, added examples

Or change the Droid class to something like this

  public Droid(Context context,int x, int y) { this.bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.birdpic); this.x = x; this.y = y; } 

Or create an application something like this:

 public class App extends android.app.Application { private static App mApp = null; /* (non-Javadoc) * @see android.app.Application#onCreate() */ @Override public void onCreate() { super.onCreate(); mApp = this; } public static Context context() { return mApp.getApplicationContext(); } } 

And call App.context () wherever you need context - note, however, that not all functions are available in the application context, some of them are available only in the context of activity, but this will certainly be related to your need for getResources ( )

Note that you will need to add android: name to your application definition in your manifest, something like this:

 <application android:icon="@drawable/icon" android:label="@string/app_name" android:name=".App" > 
+28
Nov 23 2018-11-11T00:
source share

Example: Getting the string app_name:

 Resources.getSystem().getString( R.string.app_name ) 
+21
Feb 25 '13 at 14:05
source share

This always works for me:

 import android.app.Activity; import android.content.Context; public class yourClass { Context ctx; public yourClass (Handler handler, Context context) { super(handler); ctx = context; } //Use context (ctx) in your code like this: block1 = new Droid(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.birdpic), 100, 10); //OR builder.setLargeIcon(BitmapFactory.decodeResource(ctx.getResources(), R.drawable.birdpic)); //OR final Intent intent = new Intent(ctx, MainActivity.class); //OR NotificationManager notificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE); //ETC... } 

Not related to this question, but an example using a fragment to access system resources / activity as follows:

 public boolean onQueryTextChange(String newText) { Activity activity = getActivity(); Context context = activity.getApplicationContext(); returnSomething(newText); return false; } View customerInfo = getActivity().getLayoutInflater().inflate(R.layout.main_layout_items, itemsLayout, false); itemsLayout.addView(customerInfo); 
+1
Feb 21 '15 at 12:46
source share

This is easy to do if u declared a class that extends from the application

This class will look like a singleton, so when you need context, you can get it like this:

I think this is a better answer and cleaner

Here is my code from the Utilities package:

  public static String getAppNAme(){ return MyOwnApplication.getInstance().getString(R.string.app_name); } 
0
Oct 05 '16 at 13:43 on
source share



All Articles