【问题标题】:NullPointerException on Image.createImage(path)Image.createImage(path) 上的 NullPointerException
【发布时间】:2019-11-15 04:23:51
【问题描述】:

这段代码在 for 循环中的 Image.createImage(path) 上给了我一个 NullPointerException。 怎么可能?

我使用一个数组作为名称,一个作为路径,一个ArrayList 作为按钮。

private final String[] IMG_MENU = {"/timbratura.png", "/archivio.png", "/nota_spese.png"};
private final String[] LABEL_BOTTONI_HOME = {"Timbratura", "Archivio", "Nota spese"};
private ArrayList<Button> bottoni = new ArrayList<>();

我创建了一些按钮,将图标设置为生成的图像并将其添加到ArrayList

for(int i = 0; i < LABEL_BOTTONI_HOME.length; i++) {
  Button b = new Button(LABEL_BOTTONI_HOME[i]);

  try {
      Image im = Image.createImage(IMG_MENU[i]);
      im = im.scaled(screen_width/100*20, screen_width/100*20);
      b.setIcon(im);        
  } catch (IOException ex) {
      Log.e(ex);
  }

  bottoni.add(b);
}

【问题讨论】:

    标签: ios codenameone picker


    【解决方案1】:

    检查图像是否确实存在('src' 目录并最终在 jar 中)并且具有完全相同的区分大小写的名称。我强烈建议将图像添加到资源文件中并在主题上使用getImage 从那里获取它们,因为这是一种更便携的方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-04-21
      • 2014-03-18
      • 2019-10-24
      • 2016-03-26
      • 2012-10-03
      • 1970-01-01
      • 2013-09-12
      • 2013-02-21
      相关资源
      最近更新 更多