2013-07-22 63 views
0

我有這樣的錯誤,而在Eclipse中編碼爲Android:令牌語法錯誤 「;」,期望的Java編程

語法錯誤 「;」,預計

多這條線上的標記

- Syntax error on token ")", { expected after 
this token 
- Return type for the method is missing 
- Syntax error on token ".", ... expected 
- Syntax error, insert ";" to complete 
FieldDeclaration 

而且我真的不明白爲什麼我很難確定它是什麼。 Eclipse只說出現錯誤的位置,而不是爲什麼,而其「快速修復」從不起作用。 如果有人知道如何解決這個問題plase幫我:)

package com.example.apptest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private final String defaut = "Vous devez cliquer sur le bouton « Calculer l'IMC » pour obtenir un résultat."; 
    private final String megaString = "Vous faites un poids parfait ! Wahou ! Trop fort ! On dirait Brad Pitt (si vous êtes un homme)/Angelina Jolie (si vous êtes une femme)/Willy (si vous êtes un orque) !"; 

    Button envoyer = null; 
    Button raz = null; 
    EditText poids = null; 
    EditText taille = null; 

    RadioGroup group = null; 

    TextView result = null; 

    CheckBox mega = null; /*Error here with the semicolon, eclipse wants a ','*/ 



    envoyer= (Button) findViewById(R.id.calcul); /*Long error here*/ 

    raz = (Button) findViewById(R.id.raz); 

    taille = (EditText)findViewById(R.id.taille); 
    poids = (EditText)findViewById(R.id.poids); 

    mega = (CheckBox)findViewById(R.id.mega); 

    group = (RadioGroup)findViewById(R.id.group); 

    result = (TextView)findViewById(R.id.result); 

    // On attribue un listener adapté aux vues qui en ont besoin 
    envoyer.setOnClickListener(envoyerListener); 
    raz.setOnClickListener(razListener); 
    taille.addTextChangedListener(textWatcher); 
    poids.addTextChangedListener(textWatcher); 

    // Solution avec des onKey 
    //taille.setOnKeyListener(modificationListener); 
    //poids.setOnKeyListener(modificationListener); 
    mega.setOnClickListener(checkedListener); 
} 



@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 
+4

如果將此代碼放置在適當的位置,清潔和構建應該可以工作 –

+2

首先嚐試Jigar的解決方案。然後嘗試在'onCreate()'內部放置'envoyer =(Button)findViewById(R.id.calcul);'並且查看它的工作原理。 –

+0

你可以把你的全班代碼?有時Eclipse可能會表現不正常!所以不要放棄希望。在發佈的代碼中,沒有什麼看起來錯誤的(考慮R.id.calcul是有效的)。 – blackSmith

回答

2

你在你的代碼中間的額外}讓你onCreateonCreateOptionsMenu實際上不是你的MainActivity類中

// your other code 
    mega.setOnClickListener(checkedListener); 
} //<---- End of MainActivity class 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    ... 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
    ... 
    } 

}// <--- should be actual end? 
0

你有一些缺失。

CheckBox mega = null; 

    /***** Something missing here, don't you think? *******/ 
    // (hint: maybe an onResume() method declaration) 

    envoyer= (Button) findViewById(R.id.calcul); 

如果仍然無法弄清楚,請留下評論。

0

如果不在android活動中設置內容視圖,則無法調用findViewById。所以你的代碼應該是

package com.example.apptest; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
import android.widget.Toast; 

public class MainActivity extends Activity { 
    private final String defaut = "Vous devez cliquer sur le bouton « Calculer l'IMC » pour obtenir un résultat."; 
    private final String megaString = "Vous faites un poids parfait ! Wahou ! Trop fort ! On dirait Brad Pitt (si vous êtes un homme)/Angelina Jolie (si vous êtes une femme)/Willy (si vous êtes un orque) !"; 

    Button envoyer = null; 
    Button raz = null; 
    EditText poids = null; 
    EditText taille = null; 

    RadioGroup group = null; 

    TextView result = null; 

    CheckBox mega = null; /*Error here with the semicolon, eclipse wants a ','*/ 

//move your code from here 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

// paste your moved code here 
envoyer= (Button) findViewById(R.id.calcul); /*Long error here*/ 

    raz = (Button) findViewById(R.id.raz); 

    taille = (EditText)findViewById(R.id.taille); 
    poids = (EditText)findViewById(R.id.poids); 

    mega = (CheckBox)findViewById(R.id.mega); 

    group = (RadioGroup)findViewById(R.id.group); 

    result = (TextView)findViewById(R.id.result); 

    // On attribue un listener adapté aux vues qui en ont besoin 
    envoyer.setOnClickListener(envoyerListener); 
    raz.setOnClickListener(razListener); 
    taille.addTextChangedListener(textWatcher); 
    poids.addTextChangedListener(textWatcher); 

    // Solution avec des onKey 
    //taille.setOnKeyListener(modificationListener); 
    //poids.setOnKeyListener(modificationListener); 
    mega.setOnClickListener(checkedListener); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 
0

下面的代碼在類內部浮動,沒有一個合適的方法來保存它。只需將代碼放入onCreate或其他任何方法中,就可以解決您遇到的錯誤。

envoyer= (Button) findViewById(R.id.calcul); /*Long error here*/ 

raz = (Button) findViewById(R.id.raz); 

taille = (EditText)findViewById(R.id.taille); 
poids = (EditText)findViewById(R.id.poids); 

mega = (CheckBox)findViewById(R.id.mega); 

group = (RadioGroup)findViewById(R.id.group); 

result = (TextView)findViewById(R.id.result); 

// On attribue un listener adapté aux vues qui en ont besoin 
envoyer.setOnClickListener(envoyerListener); 
raz.setOnClickListener(razListener); 
taille.addTextChangedListener(textWatcher); 
poids.addTextChangedListener(textWatcher); 

// Solution avec des onKey 
//taille.setOnKeyListener(modificationListener); 
//poids.setOnKeyListener(modificationListener); 
mega.setOnClickListener(checkedListener); 
相關問題