[英]Cannot access none static methods from static method, Android: getResources() from the type ContextWrapper
I have a static class containing overlay items that is called by my main class and then added to an overlay itself. 我有一个静态类,其中包含叠加层项目,这些叠加层项目由主类调用,然后添加到叠加层本身。
I can get this to work without image types but I would like to use them, however when I do I get the following error: Cannot make a static reference to the non-static method getResources() from the type ContextWrapper 我可以使它在没有图像类型的情况下工作,但是我想使用它们,但是当我出现以下错误时:无法从ContextWrapper类型对非静态方法getResources()进行静态引用
I have tried quite a few things to overcome this, by following some guides I have tried to add: 通过遵循我尝试添加的一些指南,我已经尝试了很多方法来克服此问题:
private static Context context;
public void onCreate(){
super.onCreate();
Mine.context = getApplicationContext();
}
public static Context getAppContext() {
return Mine.context;
}
I have also ensured that I have the class in question as an application in the manifest. 我还确保将清单中的类作为应用程序使用。
The class is as follows: 该类如下:
public static ArrayList<ExtendedOverlayItem> array = new ArrayList<ExtendedOverlayItem>();
public ArrayList<ExtendedOverlayItem> getMine() {
return array;
}
public static void addMe() {
Drawable myDrawable = getResources().getDrawable(R.drawable.draw); //This is the line that doesn't work
ExtendedOverlayItem myMarker1 = new ExtendedOverlayItem(
"sample", "sample", new GeoPoint(85.123456,
-14.123456), null);
myMarker1.setMarker(myDrawable);
myMarker1.setDescription("This is a test description");
array.add(myMarker1);
}
private static Context context;
public void onCreate(){
super.onCreate();
Mine.context = getApplicationContext();
}
public static Context getAppContext() {
return Mine.context;
}
I have tried adding the following : 我尝试添加以下内容:
myMarker1.setMarker(Mine.getAppContext().getResources().getDrawable(R.drawable.example));
But I still get null pointer errors when calling from the main method, If I leave the images out, it is called correctly. 但是从main方法调用时,我仍然会遇到空指针错误。如果我将图像遗漏在外,则可以正确调用它。
In the main method I call this class as follows: 在main方法中,我按如下方式调用此类:
Mine.addMe();
ItemizedOverlayWithBubble<ExtendedOverlayItem> thisThing = new ItemizedOverlayWithBubble<ExtendedOverlayItem>(this, Mine.array, map);
map.getOverlays().add(thisThing);
Any advice greatly appreciated. 任何建议,不胜感激。
In Java static method can not access any non-static method or variable. 在Java中,静态方法不能访问任何非静态方法或变量。
One of the basic rules of working with static methods is that you can't access a nonstatic method or field from a static method because the static method doesn't have an instance of the class to use to reference instance methods or fields.
for more information Document 有关更多信息, 文档
here is good example how to access them 这是如何访问它们的好例子
if you have context set before this line 如果在此行之前设置了上下文
Drawable myDrawable = getResources().getDrawable(R.drawable.draw);
then use 然后使用
Drawable myDrawable = context.getResources().getDrawable(R.drawable.draw);
as add me is static method and can not get context for getResources()
因为添加我是静态方法,无法获取getResources()
上下文
EDIT: 编辑:
getResources()
is called on context. getResources()
在上下文上调用。 and in your code you are calling it from a static method but static method can not access nonstatic method of your applications context. 并且在您的代码中,您是从静态方法调用它的,但是静态方法无法访问应用程序上下文的非静态方法。 so you made a static object for context and stored your context in it. 因此您为上下文创建了一个静态对象并将上下文存储在其中。 but have you checked that the context you set is not null and set before calling above line? 但是您是否在调用上述行之前检查设置的上下文是否不为null并设置了?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.