简体   繁体   中英

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

I'm brand new to programming and I've been spending some time with android. I'm trying to make a simple app (only one simple class) that calculates what you need to get on your final to get a certain grade in a class. In the 5th line, where I assign a button to calculate , I get an error that says "Multiple markers at this line

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

I was able to successfully compile something extremely similar, but I can't remember what I changed to make it not work! Here is my simple class: any help is deeply appreciated.

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

}}

EDIT: I don't currently have enough reputation to up vote all of you, but It works great now, thanks for the help. I greatly appreciate it.

Change to

@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 looks for a view with the id in the current inflated layout. So you need to initialize views after setContentView .

Also i guess you have

android:onClick="reCalc" in xml for button

So no need for your activity to implement OnClickListener and remove

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

}

onCreate() will call when application launch as you can see Android Lifecycle .

You must implement this method. The system calls this when creating your activity. Within your implementation, you should initialize the essential components of your activity. Most importantly, this is where you must call setContentView() to define the layout for the activity's user interface.

Your mistake is you are initializing your view before activity create so that will throw the error.

Even Eclipse will give you hint that you have Syntax error and all as you have mention. So you can declare your view object outside the onCreate() but you should initialize in OnCreate().

Just change this

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

all assignments/expressions in java have to be inside a block (except initializations)

you are assigning

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 , at the class level. this is wrong syntax. also you should keep in mind that even if it were allowed, your layout would not have been inflated yet and these values would have been null,

so you should shift the statements to OnCreate where you first set the content view, then call these statements:

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

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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