繁体   English   中英

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

[英]Why does java think this method is being declared, not called?

我对编程很陌生,而且我已经花了一些时间在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

}}

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

改成

@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

}

onCreate()将在应用程序启动时调用,因为您可以看到Android 生命周期

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

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

甚至Eclipse也会给你提示你有语法错误,正如你所提到的那样。 所以你可以在onCreate()之外声明你的视图对象,但你应该在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);
....
}

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第一次设置内容视图的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);

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM