我有这个令人惊讶且好看的课程:
public class SayingsHolder extends Application{
ArrayList<String> SayingsList = new ArrayList<String>(){{
SayingsList.add("1");
SayingsList.add("2");
}};
public ArrayList<String> getSayingsList() {
return SayingsList;
}
}
现在我试图通过以下方式调用我的getSayingsList
FragmentStatePagerAdapter
方法{/ 1}}。
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = (SayingsHolder).getApplication();
}
现在一切都很好,但是当我像这样编写代码时:
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = (SayingsHolder).getApplication();
holder.getSayingsList();
}
它说:
Syntax error on token "getSayingsList", Identifier expected after this token
问题在于holder
根本不起作用。我的意思是,当我输入holder.
时,getSayingsList
根本没有显示。
我在这里缺少什么?我知道这是一个非常小的问题,但似乎我无法发现它。我的活动extends FragmentActivity
,如果它很重要
似乎我在这里遗漏了一些东西。我无法在我的活动中将我的方法称为任何地方。我开始赏金,因为我需要一个例子。
答案 0 :(得分:4)
一个简单的答案是“课堂体内不允许发表声明”。对您来说,最简单的修复方法是创建一个实例列表变量,如。
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = (SayingsHolder).getApplication();
ArrayList<String> sayingsList = holder.getSayingsList();
}
这只是一个选择。您也可以将此holder.getSayingsList();
移动到方法体或静态块。
像所有其他问题一样,程序问题是语法错误。
根据Java语法,类主体可以包含成员声明,静态块,方法声明,另一个类声明,构造函数等。您可以在http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html上阅读此内容。这可能会造成混淆,因此请先参考http://docs.oracle.com/javase/specs/jls/se7/html/jls-2.html。这给出了关于每个符号的想法。
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 {
}
}
所有这些都适用于课堂内。但在您的情况下,您正在尝试向类主体添加语句。这不符合语法规则
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
}
希望这有帮助
答案 1 :(得分:2)
在类的定义中,您可以拥有方法和属性。 “holder.getSayingsList();”是一个属于方法内的语句。它不能单独定义一个类。
以下是一个例子:
public class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
final SayingsHolder holder = SayingsHolder.getApplication();
/* You cannot use holder here. Statements like this must be inside a method.
holder.getSayingsList();
*/
/* For the same reason, you cannot do this here:
System.out.println("Hello World!");
*/
/* You CAN use holder in an instance method */
public void foo() {
holder.getSayingsList();
}
/* You can use it in a class method, but you must instantiate a ScreenSlidePagerAdapter object first */
public static void main(String[] args) {
ScreenSlidePagerAdapter obj = new ScreenSlidePagerAdapter();
obj.getHolder().getSayingsList();
/* you can do this too */
obj.foo();
}
//getter for the holder variable
public SayingsHolder getHolder() {
return holder;
}
}
答案 2 :(得分:2)
您不能拥有非方法声明的语句。 “holder.getSayingsList();”语句必须在方法或静态初始化块中。