-2

我正在使用 eclipse 构建一个 Android Twitter 应用程序我试图将值传递给变量 itemOfClothing、clothingEmotion 和“用户”,但 eclipse 甚至不允许我启动具有相同名称的变量,更不用说将值传递给它们了。

我收到以下错误:

Duplicate field TwitterApp.itemOfClothing   
Duplicate field TwitterApp.clothingEmotion  
Duplicate field TwitterApp.user 
Syntax error on token ";", { expected after this token  
Syntax error, insert "}" to complete Block  

有人可以帮忙吗?

String itemOfClothing; //Item of clothing sending the message
String clothingEmotion; //Message of clothing
String user; //This comes from twitter sign in process

//将传递给变量的示例!

itemOfClothing = "pants";
clothingEmotion = "I'm feeling left in the dark";
user = "stuart";

试图将值传递给itemOfClothing,clothingEmotion和“用户”,但它不会让我。我知道这很愚蠢,但我不知道为什么。有人可以给我一个答案吗?

public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";
4

3 回答 3

3

静态变量只能引用其他静态变量。

private static String itemOfClothing; //Item of clothing sending the message
private static String clothingEmotion; //Message of clothing
private static String user; //This comes from twitter sign in process
于 2013-01-26T00:07:46.760 回答
2
public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

这是因为

temOfClothing,clothingEmotion并且user 是非静态变量,您正在尝试将其分配给 a static variable MESSAGE,因此您的编译器会抱怨

无法对非静态字段用户进行静态引用

使它们成为静态变量,您的代码就可以工作。

static String itemOfClothing = "pants";
    static String clothingEmotion = "I'm feeling left in the dark";
    static String user = "stuart";


    private static final String TWITTER_ACCESS_TOKEN_URL = "http://api.twitter.com/oauth/access_token";
    private static final String TWITTER_AUTHORZE_URL = "https://api.twitter.com/oauth/authorize";
    private static final String TWITTER_REQUEST_URL = "https://api.twitter.com/oauth/request_token";


    public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";
于 2013-01-26T00:07:31.797 回答
1

您已将这些变量声明为成员变量,但您正在尝试static使用静态初始化程序分配一个字符串变量。

静态初始化器只能访问静态变量或文字值。

您可以做的是将变量声明为静态:

static String itemOfClothing = "pants";
static String clothingEmotion = "I'm feeling left in the dark";
static String user = "stuart";

请注意,如果您不对这些变量使用静态初始化程序,它们将没有值。但是,如果要将这些值设置为程序逻辑的一部分,则要么将字符串声明为 NOT 为静态字符串,要么使用语句来分配不是“初始化程序”(声明的一部分)的值。

任何一个:

public String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";

或者:

public static String MESSAGE;

//on another line as part of a program, after the variables get values
MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing";
于 2013-01-26T00:10:12.653 回答