如何调用来自有没有上下文类getResources()?

编程入门 行业动态 更新时间:2024-10-20 05:48:23
本文介绍了如何调用来自有没有上下文类getResources()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

在我的应用我有很多课程和活动。机器人是不具有上下文一个类。 Mygame是延伸SurfaceView并实现SurfaceHolder.Callback一类。 我创建的Droid的mygame类的对象,并设置背景图片和位置吧。在code我已经为这个写在下面给出。

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);

Droid的类的构造函数如下。

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; }

在特定的情况下,我必须设置背景图片,并从itself.Here我面临着以下issue.Given Droid的类对Droid对象的位置是code片段来做到这一点。

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); }

问题是,在Droid类没有上下文。所以,我不能使用getResources()在这里。我曾尝试下面的code,但它崩溃。

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); }

任何人能帮助我。我只是想设置背景图片,并从Droid的类本身Droid的对象上。

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

推荐答案

一个上下文是一个处理系统;它提供的服务,如解决资源,从而获得对数据库的访问和preferences,等等。它是一个接口,允许访问应用特定资源和有关应用程序的环境的类和信息。你的活动和服务也扩大背景下,以它们继承了所有的方法来访问该应用程序运行环境的信息。

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

public classname(Context context, String s1) { ... }

更多推荐

如何调用来自有没有上下文类getResources()?

本文发布于:2023-11-14 09:32:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1586852.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:上下文   getResources

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!