不能referr我get方法方法、referr、get

2023-09-12 06:10:30 作者:拿什么相信爱情

我有这样惊人的,好看类:

I have this amazing and good looking class:

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 是:

Now I'm trying to call the getSayingsList method inside of my activity's FragmentStatePagerAdapter by:

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {   
    final SayingsHolder holder = (SayingsHolder).getApplication();
}

一切罚款了,但是当我让我的code是这样的:

Everything fine for now, but when I make my code like this:

 private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {   
        final SayingsHolder holder = (SayingsHolder).getApplication();
        holder.getSayingsList();
    }

这样说的:

Syntax error on token "getSayingsList", Identifier expected after this token

问题是,是不起作用的。我的意思是,当我键入这个持有人。 getSayingsList 没有显示在所有。

The problem is that holder is not functional at all. What I mean is that when i type the this holder. the getSayingsList is not showing at all.

我在想什么吗?我知道这是一个非常小的问题,但似乎我无法发现它。我的活动扩展FragmentActivity ,如果它是重要的

What am I missing here? I know that it is an extremely small issue, but it seems that I can't spot it. My activity extends FragmentActivity if it's important

看来我失去了一些东西在这里。我不能把我的活动我的方法的任何地方。我开始一个赏金,因为我需要一个例子。

It seems that I'm missing something here. I can't call my method anywhere in my activity. I'm starting a bounty, because I need an example.

推荐答案

一个简单的答案是语句是不允许在类主体。为你的程序的简单的解决办法将建立像一个实例数组变量。

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();
}

这只是其中的一个选项。您可以将这个 holder.getSayingsList(); 来一个方法体或静态块为好。 像所有其他answeres的问题,你的程序是一个语法错误。

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.

按照Java语法类的身体可以有成员声明,​​静态块,方法声明,另外一个类的声明,构造等,你可以阅读有关此上的 http://docs.oracle.com/javase/specs/jls/se7/html/jls-18.html 。这可能会造成混乱,所以先请参阅的http://文档。 oracle.com/javase/specs/jls/se7/html/jls-2.html 。这使每个符号的想法。

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

 
精彩推荐
图片推荐