【问题标题】:Why am I geting these bracket errors in my code?为什么我的代码中出现这些括号错误?
【发布时间】:2013-01-26 09:32:54
【问题描述】:

我真的不确定它为什么会这样做,但这似乎是括号的问题。 在 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;

【问题讨论】:

  • 感谢您的评论。我刚刚尝试过,但是当我这样做时,我得到了同样的错误。
  • @Luksprog.. 他们不是已经声明为字符串了吗?
  • @RohitJain 是的,我需要眼镜。
  • @mattlongman Android 标签不是必需的。这是语法问题。

标签: java eclipse syntax static-members brackets


【解决方案1】:

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

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

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";

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

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

【讨论】:

  • 没有常量。 public static final 会调用它吗?
  • @stuart.. 在变量之前使用 final 关键字会限制以后更改变量值。如果你使用public static final,变量要么在静态初始化块中赋值,要么在声明点被赋值,并与类一起加载一次。因此,它们或多或少只是常量。
  • 所以如果我写: public static String itemOfClothing = "Pants";我以后如何在 Rob 上更改此设置?
  • @stuart.. 从现在开始,您的变量不是final,您可以将其更改为包含任何字符串值。
  • 我刚刚尝试像这样更改它,但收到以下错误,有什么想法吗?抱歉,我对 Java 很陌生,但真的很想学习它。公共静态字符串 itemOfClothing = "裤子"; public static StringclothingEmotion = "我感觉自己被遗忘了";公共静态字符串用户=“StuartWestgate”;
猜你喜欢
  • 2020-06-15
  • 1970-01-01
  • 2022-01-08
  • 1970-01-01
  • 1970-01-01
  • 2019-09-17
  • 2021-03-24
  • 2016-04-08
  • 1970-01-01
相关资源
最近更新 更多