2

I made a very simple app on Android, theirs two line of code that just don't work.

The two lines

protected int images[];
images = [R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4];

Yes, I know you can make these two lines one, but in the program one line is located at the start of the class and the other is located in onCreate().

Errors:

1.Red underline under the second "[" character. Message: Unexpected Token

2.Red underline under the second "]" character. Message: ; Expected

I'm a beginner in Android Development, this would work in As3, Javascript and C++ but doesn't seem to work here.

Many of you are telling me to use {} (braces) instead of [] (square brackets) but then a big chunk of code doesn't work. Here's the part that gets the Red underline:

{R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4}

And here's the error I get: Array initializer not allowed here I'm using intelliJ if that helps. And again the first line is located at the start of the class and the other is located in 'onCreate()'.

user2990508
  • 245
  • 3
  • 11
  • you could have easily googled "how to define an int array in Java" – Michael Yaworski Nov 18 '13 at 02:31
  • 1
    " this would work in As3, Javascript and C++ but doesn't seem to work here." Perhaps because Android uses the Dalvik dialect of Java. When in Rome, do as the Romans do, but in Minneapolis, trade the pizza and toga for hotdish and a parka. – MarsAtomic Nov 18 '13 at 02:41

4 Answers4

5

change to this :

protected int [] images = new int [] { R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4 };
Hendy
  • 255
  • 1
  • 5
  • 26
4

You are wrong in syntax, do this:

protected int images[] = {R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4};

Hope this helps.

user2652394
  • 1,676
  • 1
  • 12
  • 15
2
protected int images[]; // or protected int[] images;
images = new int[]{R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4};

OR

protected int[] images = {R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4}; 
// or protected int images[] = {R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4}; 
Michael Yaworski
  • 12,398
  • 17
  • 59
  • 91
1

How about using protected int [] images; instead of protected int images[]; to declare the array? i.e:

protected int [] images = {R.drawable.loading, R.drawable.loading1, R.drawable.loading2, R.drawable.loading3, R.drawable.loading4};

Infact if you read How do I declare and initialize an array in Java? , you get to know various ways you can initialize an array in Java. Also as @user2652394 suggests ,definitely not using curly brackets in also the issue. Hope this helps.

Community
  • 1
  • 1
Shobhit Puri
  • 24,785
  • 8
  • 89
  • 115