gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 06:23:45 25 4
gpt4 key购买 nike

我是编程新手,我已经在 Android 上花了一些时间了。我正在尝试制作一个简单的应用程序(只有一个简单的类(class))来计算你需要在期末考试中获得什么才能在类(class)中获得一定的成绩。在第 5 行,我分配了一个按钮来计算,我收到一条错误消息“该行有多个标记

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

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

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 的 View 。所以你需要在setContentView之后初始化 View 。

我猜你也有

android:onClick="reCalc" in xml for button

因此您的 Activity 不需要实现 OnClickListener 并删除

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

}

关于java - 为什么java认为这个方法是被声明的,而不是被调用的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20779328/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com