为什么java认为这个方法被声明,而不是被调用?

时间:2013-12-26 04:53:59

标签: java android

我对编程很陌生,而且我已经花了一些时间在android上。我正在尝试制作一个简单的应用程序(只有一个简单的类)来计算你在决赛中获得某个等级所需要的东西。在第5行,我为calculate指定了一个按钮,我收到一条错误消息 “此行的多个标记

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

我能够成功编译一些非常相似的东西,但我不记得我改变了什么让它变得不起作用!这是我的简单课程:非常感谢任何帮助。

public class MainActivity extends Activity implements View.OnClickListener  {

Button calculate;
EditText grade, wantedGrade, finalValue;
calculate = (Button) findViewById(R.id.button1);

grade = (EditText) findViewById(R.id.editText1);
wantedGrade = (EditText) findViewById(R.id.editText2);
finalValue = (EditText) findViewById(R.id.editText3);

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

/** calculates the grade and alerts the result */
public void reCalc(View view)
{

    // calculate the grade
    // (TotalGrade - (currentGrade * .8)) / .2) = finalGrade
    // where .8 and .2 are the values of the normal semester 
    // weight and final weight, respectively
    double target;
    double current = Double.parseDouble(grade.getText().toString());
    double desired = Double.parseDouble(wantedGrade.getText().toString());
    double finalWeight = Double.parseDouble(finalValue.getText().toString()) / 100;
    double normalWeight = 1.0 - finalWeight;


    target = (desired - (current * normalWeight)) / finalWeight; 

    AlertDialog gradeMessenger = new AlertDialog.Builder(this).create();     
    gradeMessenger.setMessage("You need a " + target + " percent on the final to get a " + desired + " in the class.");
    gradeMessenger.setCancelable(true);
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}}
编辑:我目前没有足够的声誉来投票给所有人,但现在效果很好,谢谢你的帮助。我非常感激。

3 个答案:

答案 0 :(得分:1)

更改为

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main); 
    // initialize after setContentview
    calculate = (Button) findViewById(R.id.button1);

    grade = (EditText) findViewById(R.id.editText1);
    wantedGrade = (EditText) findViewById(R.id.editText2);
    finalValue = (EditText) findViewById(R.id.editText3);

}

findViewById在当前膨胀的布局中查找id为id的视图。因此,您需要在setContentView之后初始化视图。

我猜你还有

android:onClick="reCalc" in xml for button

因此,您的活动无需实施OnClickListener并删除

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

}

答案 1 :(得分:1)

onCreate()会在应用程序启动时调用,因为您可以看到Android Lifecycle

  

您必须实施此方法。系统在创建活动时调用此方法。在您的实施中,您应该初始化活动的基本组成部分。最重要的是,您必须调用setContentView()来定义活动用户界面的布局。

您的错误是您在创建活动之前初始化您的视图,以便抛出错误。

即使Eclipse会给你提示你有语法错误,并且正如你所提到的那样。 因此,您可以在onCreate()之外声明您的视图对象,但您应该在OnCreate().初始化

只需更改此

@Override
protected void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
grade = (EditText) findViewById(R.id.editText1);
wantedGrade = (EditText) findViewById(R.id.editText2);
finalValue = (EditText) findViewById(R.id.editText3);
....
}

答案 2 :(得分:0)

java中的所有赋值/表达式必须为inside a block(初始化除外)

您正在分配

calculate = (Button) findViewById(R.id.button1);

grade = (EditText) findViewById(R.id.editText1);
wantedGrade = (EditText) findViewById(R.id.editText2);
finalValue = (EditText) findViewById(R.id.editText3);

outside any code block,在班级。这是错误的语法。另外你应该记住,即使它被允许,你的布局也不会被夸大,这些值也会为空,

所以你应该shift the statements to OnCreate首先设置内容视图,然后调用这些语句:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    calculate = (Button) findViewById(R.id.button1);

    grade = (EditText) findViewById(R.id.editText1);
    wantedGrade = (EditText) findViewById(R.id.editText2);
    finalValue = (EditText) findViewById(R.id.editText3);

}