如何从没有上下文的类中调用 getResources() ?
在我的申请中,我有很多课程和活动。 Droid 是一个没有上下文的类。 Mygame 是一个扩展 SurfaceView 并实现 SurfaceHolder.Callback 的类。 我正在 mygame 类中创建一个 Droid 对象,并为其设置背景图像和位置。下面给出了我为此编写的代码。
block1 = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.birdpic), 100, 10);
下面给出了 Droid 类的构造函数。
public Droid(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}
在特定场景中,我必须从 Droid 类本身设置 Droid 对象的背景图像和位置。这里我面临着这个问题。下面给出的是执行此操作的代码片段。
if(checkflag)
{
myObj.centerblock=new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.blast), myObj.xpos, myObj.ypos);
}
问题是 Droid 类没有上下文。所以我不能在这里使用 getResources() 。我已经尝试过下面的代码,但它崩溃了。
if(checkflag)
{
myObj.centerblock=new Droid(BitmapFactory.decodeResource(myObj.getResources(), R.drawable.blast), myObj.xpos, myObj.ypos);
}
任何人都可以帮助我吗?我只想设置背景图像并将其定位到 Droid 类本身的 Droid 对象。
In my application I have many classes and activities. Droid is a class which does not have context. Mygame is a class which extends SurfaceView and implements SurfaceHolder.Callback.
I am creating an object of Droid in mygame class and setting the background image and position for it. The code I have written for this is given below.
block1 = new Droid(BitmapFactory.decodeResource(getResources(), R.drawable.birdpic), 100, 10);
The constructor of Droid class is given below.
public Droid(Bitmap bitmap, int x, int y) {
this.bitmap = bitmap;
this.x = x;
this.y = y;
}
In a particular scenario i have to set the background image and position of the Droid object from the Droid class itself.Here i am facing the issue.Given below is the code snippet to do 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. So I cannot use getResources() here. I have 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 anybody help me. I just want to set the background image and position it for the Droid object from the Droid class itself.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
上下文是系统的句柄;它提供诸如解析资源、获取数据库访问权限和首选项等服务。它是一个“接口”,允许访问应用程序特定的资源和类以及有关应用程序环境的信息。您的活动和服务还扩展了 Context,它们继承了所有这些方法来访问应用程序运行的环境信息。
这意味着如果您想获取/修改有关资源的某些特定信息,则必须将上下文传递给特定类。
您可以在构造函数中传递上下文,例如
A Context is a handle to the system; it provides services like resolving resources, obtaining access to databases and preferences, and so on. It is an "interface" that allows access to application specific resources and class and information about application environment. Your activities and services also extend Context to they inherit all those methods to access the environment information in which the application is running.
This means you must have to pass context to the specific class if you want to get/modify some specific information about the resources.
You can pass context in the constructor like
示例:获取 app_name 字符串:
Example: Getting app_name string:
对此的正常解决方案是在创建类时或在首次创建类之后但需要使用上下文之前将上下文的实例传递给类。
另一种解决方案是使用静态方法创建一个 Application 对象来访问应用程序上下文,尽管这将 Droid 对象相当紧密地耦合到代码中。
编辑,添加示例
要么将 Droid 类修改为如下所示
,要么创建一个如下所示的应用程序:
并在需要上下文的任何地方调用 App.context() - 但请注意,并非所有功能都可用在应用程序上下文中,有些仅在活动上下文中可用,但它肯定会满足您对 getResources() 的需要。
请注意,您需要将 android:name 添加到清单中的应用程序定义中,如下所示:
The normal solution to this is to pass an instance of the context to the class as you create it, or after it is first 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 that couples the Droid object fairly tightly into the code.
Edit, examples added
Either modify the Droid class to be something like this
Or create an Application something like this:
And call App.context() wherever you need a context - note however that not all functions are available on an application context, some are only available on an activity context but it will certainly do with your need for getResources().
Please note that you'll need to add android:name to your application definition in your manifest, something like this:
这总是对我有用:
与这个问题无关,但使用片段访问系统资源/活动的示例如下:
This always works for me:
Not related to this question but example using a Fragment to access system resources/activity like this:
如果你声明了一个从 Application 扩展的类,那么这很容易完成,
这个类就像一个单例,所以当你需要一个上下文时,你可以像这样得到它:
我认为这是更好的答案,更干净
这是我的实用程序包中的代码:
It can easily be done if u had declared a class that extends from Application
This class will be like a singleton, so when u need a context u can get it just like this:
I think this is the better answer and the cleaner
Here is my code from Utilities package: