A simple answer is “statements are not allowed in class body”. The simplest fix for you program would be creating an instance list variable like.
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = (SayingsHolder).getApplication();
ArrayList<String> sayingsList = holder.getSayingsList();
}
This is just one option. You can move this holder.getSayingsList();
to a method body or static block as well.
Like all the other answeres the issue with you program was a syntax error.
As per Java syntax a class body can have member declaration, static block, method declaration, another class declaration, constructor etc. You could read about this on http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html. This may be confusing so first refer http://docs.oracle.com/javase/specs/jls/se7/html/jls-2.html. This gives an idea about each notations.
public class ScreenSlidePagerAdapter {
// This is a member declaration. So "holder" is now a class member.
final SayingsHolder holder = (SayingsHolder).getApplication();
// This is a constructor
public ScreenSlidePagerAdapter(){
}
// An inner class
class InnerScreenSlidePagerAdapter{
}
// A method
public void aMethod() {
}
// This is a static block
static {
}
}
All these are valid to be used inside a class. But in your case you are trying to add a statement to a class body. That is not as per syntax rules
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = (SayingsHolder).getApplication(); //This line a member declaration. And a valid sysntax. So no issues here
holder.getSayingsList(); // This is a statement. So this line creates issue as its not part of a class body.
// Statements should be enclosed within a method body or static block or for an assigment.
//But instead if you say it like
List<String> sayingsList = holder.getSayingsList(); // Now it became a filed(member) declaration. You are trying to declare a member names "sayingsList" just like you created "holder" above
//So now we converted the statement to a member declaration
}
Hope this helps
1
solved Can’t referr to my get method