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

rAg*_*Age 1 java static android

我的简单的apliccation有问题.我不想粘贴大量的代码.我有类Zaladuj我必须使用加载文件getResources().

loading_screen = BitmapFactory.decodeResource(Widok.getResources(),R.drawable.loading_screen);
Run Code Online (Sandbox Code Playgroud)

在这一行我有错误:

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

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

Fr3*_*dan 5

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) {}
    }
}
Run Code Online (Sandbox Code Playgroud)