-3

我真的不确定为什么要这样做,但这似乎是括号的问题。在 Eclipse 中为 Android 运行这段代码时出现以下错误:

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 final String itemOfClothing;
public static final String clothingEmotion;
public static final String user;<<<<<<<<<<<<<<<<<<<<< Syntax error on token ";", { expected after this token


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

public static String MESSAGE = itemOfClothing +": " + clothingEmotion + "! #" + user + "EmotionalClothing"; <<<<<<<<<<<<<<<<<<<<<< Syntax error, insert "}" to complete Block


public TwitterApp(Activity context, String consumerKey, String secretKey) {
    this.context = context;
4

1 回答 1

2

您应该仅在声明点或在构造函数中初始化字符串。您不能在顶级课程中发表声明。你可以在那里声明。

因此,一种解决方案是,更改以下语句:-

public static final String itemOfClothing;
public static final String clothingEmotion;
public static final String user;

/** You can't have below assignments directly under the top-level class **/
itemOfClothing = "pants";
clothingEmotion = "I'm feeling left in the dark";
user = "stuart";

到: -

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

或者,另一种解决方案是,在构造函数中移动这些赋值,在这种情况下,您还必须在该构造函数中移动initializationof MESSAGE

而且,如果这些变量应该是constants,我假设它们是public static final,那么你应该使用ALL_CAPS_WITH_UNDERSCORE它们来命名它们。

于 2013-01-26T09:37:31.077 回答