[英]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.