eclipse 标记“;”上的语法错误,预期的 Java 编程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17796234/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-19 20:56:26  来源:igfitidea点击:

Syntax error on token ";", , expected Java Programming

javaandroideclipsetoken

提问by Jo Colina

I have this error while coding in Eclipse for android:

在 Eclipse for android 中编码时出现此错误:

Syntax error on token ";", , expected

标记“;”的语法错误,,预期

and

Multiple markers at this line

此行有多个标记

- 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

And I don't really see why I'm having so much trouble figuring what it is. Eclipse only says where there is an error, not why, and its "Quick fixes" never work. Does anyone know how to fix this, please?

我真的不明白为什么我很难弄清楚它是什么。Eclipse 只说明有错误的地方,而不是错误原因,而且它的“快速修复”永远不会起作用。请问有人知道怎么解决吗?

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

}

回答by Java Devil

You have an extra }in the middle of your code so your onCreateand onCreateOptionsMenuare not actually inside your MainActivityclass

你有一个额外的}在你的代码的中间让你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?

回答by Nishanthi Grashia

The below code is floating inside the class without a proper method to hold it. Just put the code within the onCreate or any other method, and the error you get would be resolved.

下面的代码在类中浮动,没有适当的方法来保存它。只需将代码放在 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);

回答by ADTC

You have something missing.

你缺少一些东西。

  CheckBox mega = null;

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

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

Leave a comment if you still can't figure it out.

如果您仍然无法弄清楚,请发表评论。

回答by Talha

You can not call findViewById without setting content view in android activity. so your code should be

您不能在 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;
}

}