-2

eclipse を使用して Android の Twitter アプリを作成しています。変数 itemOfClothing、clothingEmotion、および "user" に値を渡そうとしていますが、値を渡すだけでなく、同じ名前の変数を開始することさえできません。

次のエラーが表示されます。

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

その理由は

temOfClothingclothingEmotionおよびuser非静的変数であり、それを に割り当てようとしている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";

これらの変数で静的イニシャライザを使用しない場合、値がないことに注意してください。ただし、これらの値をプログラム ロジックの一部として設定する場合は、文字列が静的文字列ではないことを宣言するか、ステートメントを使用して "初期化子" (宣言の一部) ではない値を割り当てます。

また:

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 に答える