【问题标题】:Eclipse reporting errors on arrays for AndroidEclipse 报告 Android 阵列上的错误
【发布时间】:2011-03-29 01:09:01
【问题描述】:

我正在尝试为我正在开发的游戏初始化一个 3D 数组,经过多次语法更改后,我无法弄清楚如何让它工作!我一开始是:

public class AnimationView extends SurfaceView implements SurfaceHolder.Callback {//Create bitmaps.
Bitmap bitmapGoal = BitmapFactory.decodeResource(this.getResources(), R.drawable.goal);
Bitmap bitmapOrig = BitmapFactory.decodeResource(this.getResources(), R.drawable.ball);
Bitmap bitmap = Bitmap.createScaledBitmap(bitmapOrig, 150, 150, true);
//initialize the canvas.
private Canvas c;
private int score[] = {0, 0, 0, 0};
public int numBalls = 1;
//we support up to 4 balls. thus each array is 4 bit.
private int ballX[] = {0, 200, 400, 600};
private double ballY[] = {0, 0, 0, 0};
private double dirV[] = {0, 0, 0, 0};
private int dirH[] = {30, 30, 30, 30};
private static final int SCALE = 10;
private double elasticity = .6;
private int rotationNow[] = {5, 5, 5, 5};
private int rotationDraw[] = {0, 0, 0, 0};


class AnimationThread extends Thread {
//Are we running currently?
private boolean mRun;
    //layer 1 is how many balls, 4 layers deep.
    //layer 2 is which ball we're talking about, either 1, 2, 3, or 4 layers deep, depending on layer 1.
    //layer 3 is the bounds of the ball, dependent on how many there are total.
    //layer 3 is formatted x-min, x-max, y-min, y-max
    int[][][] bounds = new int[][][] {
        { {0, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        //end first layer
        { {0, c.getWidth() / 2 - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() / 2, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0}, {0, 0, 0, 0} }, 
        //end second layer
        { {0, c.getWidth() / 3 - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() / 3, c.getWidth() * 2 / 3  - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {c.getWidth() * 2 / 3, c.getWidth() - bitmap.getWidth(), 0, c.getHeight() - bitmap.getHeight()}, {0, 0, 0, 0} }, 
        //end third layer
        { {0, c.getWidth() / 2, 0, c.getHeight() / 2}, {c.getWidth() / 2, c.getWidth(), 0, c.getHeight() / 2}, {0, c.getWidth() / 2, c.getHeight() / 2, c.getHeight()}, {c.getWidth() / 2, c.getWidth(), c.getHeight() / 2, c.getHeight()} }
        //end fourth, and final layer!
    };

对于奇怪的格式错误,我们深表歉意。我知道这没有任何帮助。在 ymax 和 int[][][] 之间有一个新行。 您并不完全需要仔细阅读并理解它,但它会编译然后在执行过程中出错。于是我尝试制作一个简单的 3D 数组,我从以下内容开始:

int[][][] bounds = new int[1][1][1];
bounds[0][0][0] = 0;

Eclipse 在第一行的分号下有红色波浪状的。说 '令牌“;”上的语法错误,{ 应在此令牌之后'

这就是令人沮丧的地方。因为将完全相同的代码复制/粘贴到常规 Java 程序中工作正常,但我无法让它在 Android 项目中工作。然后我将一些东西简化为:

int[] bounds = new int[1];
bounds[0] = 0;

完全相同的错误,完全相同的位置!为什么是日食??我也尝试使用“int bounds[][][]”而不是“int[][][] bounds”,但没有区别,仍然是同样的错误。

我已经重新启动了我的计算机,多次清理了我的项目,重新启动了 Eclipse。我没主意了。你有什么??

【问题讨论】:

  • 我们能看到更多代码吗? (可能是int[][][] bounds... 之前的内容)
  • 一般来说,一个未闭合的括号或缺少分号before这个语句会导致这样的错误,因为解析器认为这一行也是前一行的一部分。
  • @euphoria83 看到我的回答。通常是在这句话之后。

标签: java android arrays eclipse


【解决方案1】:

好吧,似乎问题不在之前,而是在您粘贴的代码之后。 这个分配 - bounds[0][0][0] = 0; 可能没有任何方法,这是非法的。当 Eclipse 看到需要在方法中的表达式时,它期望上面的行是方法声明,因此它期望 '{' 作为方法块的开头,而不是 ';'

【讨论】:

    【解决方案2】:

    好吧,我觉得很可笑。在对为什么变量无法初始化以及为什么非常简单的代码无法编译感到非常非常困惑之后。事实证明,尽管画布和位图可用,但实际上它们将空值返回到数组中。

    所以我现在开始工作了。

    此外,对于我在这里的第一个问题,我对解决方案的速度印象深刻。非常感谢!

    【讨论】:

      【解决方案3】:
      int[][][] bounds = new int[1][1][1];
      
      bounds[0][0][0] = 0;
      

      我复制了这两行,似乎编译得很好。 我想你可能忘了评论之前的 bounds 声明。 (或)您可能缺少大括号或类似的东西

      【讨论】:

        猜你喜欢
        • 2015-05-19
        • 2013-03-07
        • 1970-01-01
        • 2018-12-28
        • 2013-10-05
        • 1970-01-01
        • 2011-12-13
        • 2020-03-27
        • 2012-10-31
        相关资源
        最近更新 更多