出现错误:无法从静态上下文中引用非静态方法'getResources()'

FelinaSaber:

我正在使用Java在Android Studio上开发一个rpg游戏,并且在Resource Manager中的Drawable中有png图片。我正在尝试将drawable转换为bitmap,但出现错误。

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

public class Player implements GameObject{
    private int direction;
    private boolean move;

    public Player(int direction, boolean move){//constructior
        this.direction = direction;
        this.move = move;

        Drawable myDrawable = MainActivity.getResources().getDrawable(R.drawable.down_standing);
        Bitmap downStanding      = ((BitmapDrawable) myDrawable).getBitmap();
    }

    @Override
    public void draw(Canvas canvas){

    }

    @Override
    public void update(){

    }

    public void update(int direction, boolean move){
        this.direction = direction;
        this.move = move;
    }
}

至于MainActivity:


import androidx.appcompat.app.AppCompatActivity;

import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.Window;
import android.view.WindowManager;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);
        Constants.SCREEN_WIDTH = dm.widthPixels;
        Constants.SCREEN_HEIGHT = dm.heightPixels;

        setContentView(new GamePanel(this));
    }
}

请帮忙。非常感谢!!

卢卡·穆拉(Luca Murra):

要打电话给getResources()您一个Context参考,您不能静态地调用它,所以这是错误的:

MainActivity.getResources()

要实现它,您应该提供一个Context参数,您可以从一个Activity中获得它:

public Player(int direction, boolean move, Context context){  //constructior with Context
    this.direction = direction;
    this.move = move;

    Drawable myDrawable = context.getResources().getDrawable(R.drawable.down_standing);  // This is right
    Bitmap downStanding = ((BitmapDrawable) myDrawable).getBitmap();
}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误:无法从静态上下文中引用非静态方法'findViewById(int)'

错误:无法在静态上下文中引用非静态方法“ edit”

错误:无法从静态上下文中引用非静态方法 setListMovie(ArrayList<Movie>)

错误:无法从静态上下文中引用非静态方法 getActivity()

“无法从静态上下文引用非静态方法”错误

我在animate()中遇到错误,这是“错误:无法从静态上下文中引用非静态方法animate()”

无法在非静态上下文中访问静态方法

错误:无法从Java的静态上下文中引用非静态变量扫描

如何解决此错误?:无法从静态上下文中引用非静态方法 maxOf(int,int)

Android:非静态方法无法从静态上下文中引用。困惑?

“无法从静态上下文中引用非静态方法”背后的原因是什么?

无法从静态上下文中引用luckyperms 非静态方法getUserManager()

无法从静态上下文引用Java非静态方法

setDisplayHomeAsUpEnabled()错误:无法从静态上下文引用非静态方法

错误:无法从静态上下文引用非静态方法unbind()

使用.setText在jTextArea上打印将返回错误非静态变量,无法从静态上下文中引用

java:不能从静态上下文中引用非静态变量错误

试图让我的显示方法工作(不能从静态上下文中引用非静态方法)

从静态上下文中的 a 调用非静态方法

从非静态上下文中调用静态方法

非静态方法不能从静态上下文中引用

非静态方法不能从静态上下文中引用

非静态方法getIntent()不能从静态上下文中引用

不能从静态上下文中引用非静态方法 matcher(CharSequence)

的java - 非静态方法“getLogger”不能从静态上下文中引用

非静态方法count(int)不能从静态上下文中引用

非静态方法不能从静态上下文中的java 8流引用

JAVA - 不能从静态上下文中引用非静态方法 add(E)

非静态方法<T> findViewById(int)不能从静态上下文中引用