Reputation: 1041
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!
Upvotes: 0
Views: 6976
Reputation: 1
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
Upvotes: 0
Reputation: 44571
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
Upvotes: 5