为什么图形崩溃会采取任何措施? [英] Why any action with Graphics crashing?

查看:107
本文介绍了为什么图形崩溃会采取任何措施?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此代码在蓝色背景上显示光标(精灵).
我尝试移动光标失败,但是出现空指针异常",
因为g = null,所以我不知道为什么&如何修复.
请帮忙.

this code displaying cursor(sprite) on blue background.
I unsuccessfully trying to move cursor, but got "Null Pointer Exception",
because g = null, I don''t know why & how to fix.
Please, help.

package hello;
 
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.game.Sprite;
import javax.microedition.lcdui.game.LayerManager;
import javax.microedition.lcdui.Graphics;
 
public class Tiles extends MIDlet {
 
  Board b;
 
  public Tiles() {
    b = new Board(this);
  }
 
  public void startApp() {
    Display.getDisplay(this).setCurrent(b);
  }
 
  public void pauseApp() {
  }
 
  public void destroyApp(boolean unconditional) {
  }
 
}
 
class Board extends Canvas implements CommandListener {
private GameDesign gameDesign;
private Sprite cursor;
private LayerManager lm;
 
public Graphics g;
public int x = 0;
  MIDlet midlet;
 
  Command exitCommand;
 
  public Board(MIDlet midlet_) {
      try{
          this.lm = new LayerManager();
 
this.gameDesign = new GameDesign();
this.cursor = this.gameDesign.getCurs();
this.cursor.defineReferencePixel(cursor.getWidth(), cursor.getHeight());
this.cursor.move(0, 0);
          this.lm.append(cursor);
          
          this.lm.setViewWindow(0, 0, this.getWidth(), this.getHeight());
      }
      catch(Exception x){}
 
    exitCommand = new Command("Exit", Command.SCREEN, 2);
    addCommand(exitCommand);
 
    setCommandListener(this);
 
    ////////////////////===============
 
 
    repaint();
 
  }
 
  public void commandAction(Command c, Displayable d) {
    if (c == exitCommand) {
      midlet.notifyDestroyed();
 
    }
  }
 
  public void paint(Graphics g) {
      g.setColor(255);
      g.fillRect(0, 0, 100, 100);
            //g.drawImage(im, x, 10, Graphics.LEFT | Graphics.TOP);
            this.cursor.paint(g);
  }
 
  public void keyPressed(int code) {
    int game = getGameAction(code);
 
    switch (game) {
    case Canvas.UP:
      System.out.println("Canvas.UP");
      break;
    case Canvas.DOWN:
      System.out.println("Canvas.DOWN");
      break;
    case Canvas.LEFT:
      System.out.println("Canvas.LEFT");
      break;
    case Canvas.RIGHT:
        x++;
        this.cursor.setPosition(x, cursor.getY());
        this.cursor.paint(g);      //<===============NULL POINTER EXCEPTION
      System.out.println("Canvas.RIGHT");
      break;
    }
 
  }
 
 
}

推荐答案

问题始于名为 g 的两个变量.
paint(Graphics g)中的一个是瞬态的,只能在此调用内使用以进行绘制. [请参阅来自JSR-118帮助的Canvas.paint()引用:

在paint()调用返回之后,对此图形对象的操作是不确定的.因此,应用程序不得缓存此Graphics对象供以后使用或由另一个线程使用.只能在此方法的范围内使用.
]
全局Graphics g从未初始化,这就是为什么在尝试使用它时会出现空指针异常的原因.

您应该做的是调用Board.repaint()Board.repaint(x, y, width, height)代替失败的行.这是从Canvas继承的方法,最终将导致调用您的 paint()来完成工作.请参见Canvas类及其方法的JSR-118描述.此外,还可以从诺基亚或摩托罗拉开发者论坛等地方查看示例程序.

[提示:如果您想回复此内容,请不要再发表另一个答案",请对此发表评论.如果您喜欢此答案,请对其投票并标记为已接受".谢谢.
The problem starts with the two variables called g.
The one in paint(Graphics g) is transient, and can only be used inside this call to paint. [See this quote from the JSR-118 help for Canvas.paint():

Operations on this graphics object after the paint() call returns are undefined. Thus, the application must not cache this Graphics object for later use or use by another thread. It must only be used within the scope of this method.
]
Your global Graphics g is never initialised, which is why you get a null pointer exception when you try to use it.

What you should be doing is calling Board.repaint() or Board.repaint(x, y, width, height) in place of the failing line. This is a method inherited from Canvas which will ultimately result in your paint() being called to do the work. See the JSR-118 description of the Canvas class and its methods. Also, look at sample programs from places like the Nokia or Motorola developers forums.

[Hint: if you want to reply to this, don''t post another ''Answer'', make a comment to this one. If you like this answer, vote for it and mark it ''accepted''. Thanks.


这篇关于为什么图形崩溃会采取任何措施?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆