0

I have searched the internet high and low for the answer to this error:

Non-static method 'getStringExtra(java.lang.String)' cannot be referenced from a static context

I have not found anything so I have come here. Here is the code I use for adding the info as extras:

Intent OpenList = new Intent(this, ListRandom.class);
OpenList.putExtra("ListItem1",List.get(1));
OpenList.putExtra("ListItem2", List.get(2));
OpenList.putExtra("ListItem3", List.get(3));
OpenList.putExtra("ListItem4",List.get(4));
OpenList.putExtra("ListItem5", List.get(5));

And here is getting the Extras, where I get the error:

    Intent OpenList = getIntent();
    ListItem1 = Intent.getStringExtra("ListItem1");
    ListItem2 = Intent.getStringExtra("ListItem2");
    ListItem3 = Intent.getStringExtra("ListItem3");
    ListItem4 = Intent.getStringExtra("ListItem4");
    ListItem5 = Intent.getStringExtra("ListItem5");

Any help would be appreciated as I am growing as a programmer!

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Andy Roy D.
  • 1,001
  • 1
  • 8
  • 7
  • 4
    duplicate of [What is the reason behind "non-static method cannot be referenced from a static context"?](http://stackoverflow.com/questions/290884/what-is-the-reason-behind-non-static-method-cannot-be-referenced-from-a-static) and http://stackoverflow.com/questions/4922145/non-static-method-cannot-be-referenced-from-a-static-context-error and http://stackoverflow.com/questions/2694566/non-static-method-cannot-be-referenced-from-a-static-context and http://stackoverflow.com/questions/5972764/non-static-method-cannot-be-referenced-from-a-static-context – CommonsWare Jun 17 '13 at 23:03

2 Answers2

5

Change

 Intent OpenList = getIntent();
ListItem1 = Intent.getStringExtra("ListItem1");
ListItem2 = Intent.getStringExtra("ListItem2");
ListItem3 = Intent.getStringExtra("ListItem3");
ListItem4 = Intent.getStringExtra("ListItem4");
ListItem5 = Intent.getStringExtra("ListItem5");

to

 Intent OpenList = getIntent();
ListItem1 = OpenList.getStringExtra("ListItem1");
ListItem2 = OpenList.getStringExtra("ListItem2");
ListItem3 = OpenList.getStringExtra("ListItem3");
ListItem4 = OpenList.getStringExtra("ListItem4");
ListItem5 = OpenList.getStringExtra("ListItem5");

Use the Intent object that you created here

Intent OpenList = getIntent();

Just like any other class, using Intent.getStringExtra("words"); is calling it in a static way and if you look at the Intent Docs getStringExtra(String name) is not a static method so you create an instance if Intent with Intent OpenLIst = getIntent();

Also, to stick with Java programming standards you should use mixed-case for your variable names so OpenList would be openList and ListItem1 would be listItem1. This isn't necessary for compiling obviously but its a good idea to try and stick to standards

codeMagic
  • 44,549
  • 13
  • 77
  • 93
0

When you write:

    ListItem1 = Intent.getStringExtra("ListItem1");

You are using a method (i.e. getStringExtra(String name)) on a class (i.e. Intent), which is a static approach.

Instead, you should use a method on an object (i.e. OpenList) for a non-static approach.

Thus, your code should be changed to:

    ListItem1 = OpenList.getStringExtra("ListItem1");

And be applied to ListItem2, ListItem3, and so on.

OpenList