java Android:无法从类型 View 对非静态方法 getResources() 进行静态引用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12513422/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 09:11:33  来源:igfitidea点击:

Android: Cannot make a static reference to the non-static method getResources() from the type View

javaandroidstatic

提问by rAge

I've got problem with my simple apliccation. I don't want paste tons of code. I've got class Zaladujwhere i must load files using getResources().

我的简单应用程序有问题。我不想粘贴大量代码。我有课程Zaladuj,我必须使用getResources().

loading_screen = BitmapFactory.decodeResource(Widok.getResources(),R.drawable.loading_screen);

On this line i've got error:

在这一行我有错误:

Cannot make a static reference to the non-static method getResources() from the type View.

无法从类型 View 对非静态方法 getResources() 进行静态引用。

I put all classes codes here: http://www.pcyra.pl/badpanda
It's not a website, it's just folder with my project. I create instance of View in MainActivity, The error shows in class Zaladujon line 50.

我把所有的课程代码放在这里:http: //www.pcyra.pl/badpanda
这不是一个网站,它只是我的项目的文件夹。我在 MainActivity 中创建了 View 的实例,错误显示在Zaladuj第 50 行的类中。

回答by Fr33dan

getResources()is not a static method. You need an instance of the View(or Widokin your case) to call it. Your best bet would be to add a parameter to your Zaladujclass that accepts a Contextthen use getResources()on that:

getResources()不是静态方法。您需要一个View(或Widok在您的情况下)的实例来调用它。您最好的选择是向您的Zaladuj类添加一个参数,该参数接受Context然后使用getResources()

private Context context;
public Zaladuj(Context con)
{
    super()
    this.context = con
}

private void downloadResources() 
{
    //
      loading_screen = BitmapFactory.decodeResource(this.context.getResources(),R.drawable.loading_screen);
    //
    int count = 10;
    for (int i = 0; i < count; i++) 
    {
        try { Thread.sleep(1000); } catch (InterruptedException ignore) {}
    }
}

回答by Keppil

Since the getResources()method isn't static, you will need to create an instance of your Widokclass to be able to call it, eg:

由于该getResources()方法不是static,您将需要创建您的Widok类的一个实例才能调用它,例如:

Widok widok = new Widok(context);
BitmapFactory.decodeResource(widok.getResources(),R.drawable.loading_screen);

回答by Vipul Shah

The error is clearly telling you whats wrong.You are not allowed to call it with Widok.

错误清楚地告诉你出了什么问题。你不能用 Widok 调用它。

If you are in activity class then call

如果你在活动课然后打电话

BitmapFactory.decodeResource(getResources(),R.drawable.loading_screen);

If you are in non-activity class then you will need Activity context.

如果您在非活动类中,那么您将需要活动上下文。

Like if you are in AsyncTask then you would write something like this

就像如果你在 AsyncTask 那么你会写这样的东西

BitmapFactory.decodeResource(context.getResources(),R.drawable.loading_screen);

Just make sure you pass correct activity context in constructor.

只要确保在构造函数中传递正确的活动上下文。

回答by zabawaba99

The reason you're getting that error "Cannot make a static reference to the non-static method getResources() from the type View." is because the getResources() method is not a static method. In other words, you'll need to isntantiate a new Widok

您收到该错误的原因是“无法从类型视图对非静态方法 getResources() 进行静态引用。” 是因为 getResources() 方法不是静态方法。换句话说,您需要创建一个新的 Widok

Widok w = new Widok(context);

asnd then call the resources on that object

然后调用该对象上的资源

w.getResource();

**Edit

**编辑

This is the code you'd put in your activity class. You pass the keyword "this" to the constructor of Widok which references the application context

这是您放在活动类中的代码。您将关键字“this”传递给引用应用程序上下文的 Widok 的构造函数

Widok w = new Widok(this);
loading_screen = BitmapFactory.decodeResource(w.getResources(),R.drawable.loading_screen);