简体   繁体   English

无法从静态方法Android访问任何静态方法:ContextWrapper类型的getResources()

[英]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.

相关问题 获取错误:无法从静态上下文中引用非静态方法“getResources()” - getting Error: non-static method 'getResources()' cannot be referenced from a static context 无法从Scala访问Java静态方法 - Cannot access a Java static method from Scala Android:无法从静态上下文引用非静态方法 - Android: Non-static method cannot be referenced from a static context android jni从静态到非静态方法 - android jni from static to non static methods 从公共静态访问私有静态方法 - private static method access from public static “方法 main 不能声明为静态; 静态方法只能在静态或顶级类型中声明” - “The method main cannot be declared static; static methods can only be declared in a static or top level type” 无法访问从非静态到非静态类/方法的方法调用 - Cannot access a method call from non-static to non-static class/method 无法解析webView且无法从Activity类型静态引用非静态方法findViewById(int) - webView cannot be resolved & cannot make a static reference to the non-static method findViewById(int) from the type Activity 如何解决无法从ContextWrapper错误中降低继承方法的可见性? - How to fix Cannot reduce the visibility of the inherited method from ContextWrapper error? 方法main不能声明为静态; 静态方法只能以静态或顶级类型声明 - The method main cannot be declared static; static methods can only be declared in a static or top level type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM