Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android:使用Android studio使用位图显示图像时出错_Java_Android_Eclipse_Image_Bitmap - Fatal编程技术网

Java Android:使用Android studio使用位图显示图像时出错

Java Android:使用Android studio使用位图显示图像时出错,java,android,eclipse,image,bitmap,Java,Android,Eclipse,Image,Bitmap,我已经创建了一个10x10的网格,现在我想在其中一个网格方格中放置一个玩家的图像。要做到这一点,我想我必须使用位图?我会把我所有的代码放在下面。无论如何,为了尝试这样做,我创建了一个游戏类,它设置并获取玩家图像的x位置。然后,我创建了一个播放器类,在这个类中,我尝试使用Bitmap Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sokobanplayer1)添加图像; 我一直在getResources下得到一个错误,所

我已经创建了一个10x10的网格,现在我想在其中一个网格方格中放置一个玩家的图像。要做到这一点,我想我必须使用位图?我会把我所有的代码放在下面。无论如何,为了尝试这样做,我创建了一个游戏类,它设置并获取玩家图像的x位置。然后,我创建了一个播放器类,在这个类中,我尝试使用Bitmap Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sokobanplayer1)添加图像; 我一直在getResources下得到一个错误,所以我在游戏中创建了一个getResources方法,但它并没有解决这个问题。谁能告诉我怎么做,我的代码如下

//DRAW CLASS
public class Draw extends View {
Paint red = new Paint();
Paint green = new Paint();

int rectSide = 1000;

public Draw(Context context) {
    super(context);
    red.setColor(Color.RED);
    green.setColor(Color.GREEN);
    red.setStrokeWidth(8);
    green.setStrokeWidth(8);
}

public void drawGrid(Canvas canvas) {

    int width = canvas.getWidth();
    int height = canvas.getHeight();


    float startX = (width / 2) - (rectSide / 2);
    float stopX = (width / 2) + (rectSide / 2);
    float startY = (height / 2) - (rectSide / 2);
    float stopY = (height / 2) + (rectSide / 2);

    for (int i = 0; i < 1100; i += 100) {
        float newY = startY + i;
        canvas.drawLine(startX, newY, stopX, newY, green);
    }

    for (int i = 0; i < 1100; i += 100) {

        float newX = startX + i;
        canvas.drawLine(newX, startY, newX, stopY, green);
    }
}

@Override
public void onDraw(Canvas canvas) {

    drawGrid(canvas);
}
}

//GAME CLASS
public class Game {

Bitmap image;
int x;
int y;
int height;
int width;


public void setX(int x){
    this.x = x;
}

public void setY(int y){
    this.y = y;
}

public int getX(){
    return x;
}

public int getY(){
    return y;
}

public int getHeight(){
    return height;
}

public int getWidth(){
    return width;
}

public Bitmap getResources(){
    return image;
}

}


//PLAYER CLASS
public class Player extends Game {

public Player(Bitmap res, int w, int h){

    image = res;
    x = 300;
    y = 300;
    height = h;
    width = w;

    Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sokobanplayer1);
}
}


//MAIN ACTIVITY
public class MainActivity extends Activity {
Draw draw;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.activity_main);

    draw = new Draw(this);
    draw.setBackgroundColor(Color.BLUE);
    setContentView(draw);

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}


}
//绘图类
公共类绘制扩展视图{
油漆红色=新油漆();
油漆绿色=新油漆();
int-rectSide=1000;
公共绘图(上下文){
超级(上下文);
红色。设置颜色(颜色。红色);
绿色。设置颜色(颜色。绿色);
红色。设定行程宽度(8);
绿色。设置行程宽度(8);
}
公共void drawGrid(画布){
int width=canvas.getWidth();
int height=canvas.getHeight();
浮点数startX=(宽度/2)-(矩形/2);
浮动止动块X=(宽度/2)+(矩形侧/2);
浮动起点=(高度/2)-(矩形/2);
浮式采矿法=(高度/2)+(直线侧/2);
对于(int i=0;i<1100;i+=100){
float newY=startY+i;
帆布抽绳(startX、newY、stopX、newY、green);
}
对于(int i=0;i<1100;i+=100){
float newX=startX+i;
帆布。抽绳(纽克斯、斯塔蒂、纽克斯、斯托比、绿色);
}
}
@凌驾
公共空白onDraw(画布){
绘图网格(画布);
}
}
//游戏课
公开课游戏{
位图图像;
int x;
int-y;
内部高度;
整数宽度;
公共无效集合x(整数x){
这个.x=x;
}
公共空间设置(整数y){
这个。y=y;
}
公共int getX(){
返回x;
}
公共int getY(){
返回y;
}
公共整数getHeight(){
返回高度;
}
公共int getWidth(){
返回宽度;
}
公共位图获取资源(){
返回图像;
}
}
//球员级别
公共类玩家扩展游戏{
公共播放器(位图分辨率、整数w、整数h){
图像=分辨率;
x=300;
y=300;
高度=h;
宽度=w;
位图Bitmap=BitmapFactory.decodeResource(getResources(),R.drawable.sokobanplayer1);
}
}
//主要活动
公共类MainActivity扩展了活动{
抽签;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
绘制=新绘制(本);
draw.setBackgroundColor(颜色:蓝色);
setContentView(绘制);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
int id=item.getItemId();
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
}

用以下内容替换您的玩家职业:

//PLAYER CLASS
class Player extends Game {

    public Player(Context context, int w, int h){

        x = 300;
        y = 300;
        height = h;
        width = w;

        image = BitmapFactory.decodeResource(context.getResources(), R.drawable.sokobanplayer1);
    }
}

pass context.getResources()将上下文传递给播放器类的构造函数以消除错误,但图像不会显示在网格上?为什么会这样?