0

I have the following code.

public class Start extends ActionBarActivity {

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

    ArrayList<String> aPlayerList = getIntent().getStringArrayListExtra("playerList");
    static ArrayList<Integer> aScores = getIntent().getIntegerArrayListExtra("scores");
...

I get an error when trying to make the ArrayList<Integer> aScores static: Non-static method getIntent() cannot be referenced from a static context , and I don't know how to fix this.

If it helps, this is how the intent was passed:

            Bundle bund = new Bundle();
            bund.putStringArrayList("playerList", playerList);
            bund.putIntegerArrayList("scores", scores);

            Intent intent = new Intent(Players.this, Start.class);
            intent.putExtras(bund);
            startActivity(intent);

Any help would be appreciated, and if you could add the code that would fix it that would be great. Thanks,

Mostafa Saadat
  • 101
  • 1
  • 12

3 Answers3

2

Because your syntax is wrong.

You can't make a variable inside a method static what would be the use of this? Static means that the field is related to the class so you can access it without any reference (ClassName.staticField).

Variables inside methods are related to the method, so you can't access them outside it so how static could be used here?

Are you sure you don't get confused with final? Which is a valid here.


To resolve your problem, you just need to make static ArrayList<Integer> aScores as field of the class so you can access it anywhere in your code. Then edit your onCreate method to this

aScores = getIntent().getIntegerArrayListExtra("scores");

so it will save the array list inside aScores field.

Marco Acierno
  • 14,682
  • 8
  • 43
  • 53
  • If I use final, would I still be able to change the values in the arrayList using aScores.set(position, newScore)? – Mostafa Saadat Jun 09 '14 at 22:58
  • Ehm, even if static was legal `.set` was still possible. What you want is related to mutable and immutable collections. To fix your problem do: `ArrayList aScores = Collections.unmodifiableList(getIntent().getIntegerArrayListExtra("scores"));` So you wrap the `ArrayList` inside a unmodifiable list so `set` methods (etc.) will throw you an exception if used. – Marco Acierno Jun 09 '14 at 23:01
  • But I want to be able to modify the list, that's why I don't want to make it final. I need to to be static so I can use it inside of a for loop that I have later in the code, and I have the .set inside of the for loop. Sorry for this, I'm fairly new. – Mostafa Saadat Jun 09 '14 at 23:05
  • Then you need to make static yes, but a static field if you want to make it accessible everywhere. So make a field outside the class and save the reference here. (warning, this approach would let to you more bugs if you are not careful.) Put `public static ArrayList aScores` outside the class and inside the `onCreate` method just `aScores = ...` – Marco Acierno Jun 09 '14 at 23:07
  • outside the method* as field of the class. – Marco Acierno Jun 09 '14 at 23:13
  • Alright, thanks, you mind editing that into your original solution so I can mark it as the best answer? – Mostafa Saadat Jun 09 '14 at 23:16
  • Done, just a hint for your next question: When you post the question write what have you tried and what are you trying to do so next time you will get better answer in less time... – Marco Acierno Jun 09 '14 at 23:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/55348/discussion-between-mostafa-saadat-and-marco-acierno). – Mostafa Saadat Jun 09 '14 at 23:22
1

That is because getIntent() is a non static method and should not be referenced to a static field.

solution:

Remove the static of your arrayList.

Rod_Algonquin
  • 26,074
  • 6
  • 52
  • 63
0

Static methods get created only once, so you can't reference getIntent() because Java doesn't know which instance of the method you are referencing.

Check here for more information on how static methods work.

Community
  • 1
  • 1
nick
  • 2,256
  • 2
  • 14
  • 14