我写了这个方法,但不断收到错误消息:
令牌“int”的语法错误,@预期
语法错误,插入“EnumBody”以完成 BlockStatement
@Override
public int calculateCurrency()
{
int[] currency = getResources().getIntArray(R.array.currenciesvalues);
return Integer.parseInt((String) spinner1.getSelectedItem()) / Integer.parseInt((String)spinner2.getSelectedItem());
}
这是该方法所在的完整 java 类。
package com.msc.conv;
import java.text.DecimalFormat;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class CurrencyActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.currency);
final Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
final Spinner spinner2 = (Spinner) findViewById(R.id.spinner2);
final EditText etResult = (EditText) findViewById(R.id.etResult);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.currencies, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter);
spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
//public class MyOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
//String str = parent.getItemAtPosition(pos).toString();
//etResult.setText(str);
}
@Override
public void onNothingSelected(AdapterView parent){
//Do nothing
}
});
spinner2.setAdapter(adapter);
spinner2.setOnItemSelectedListener(new OnItemSelectedListener(){
//public class MyOnItemSelectedListener implements OnItemSelectedListener{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id){
//String str = parent.getItemAtPosition(pos).toString();
//etResult.setText(str);
}
@Override
public void onNothingSelected(AdapterView parent){
//Do nothing
}
});
final EditText editCurr = (EditText) findViewById(R.id.etamount);
Button convert = (Button) findViewById(R.id.btn_convert);
convert.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
double curr = Double.valueOf(editCurr.getText().toString());
double result2 = Double.valueOf(spinner1.toString()) / Double.valueOf(spinner2.toString());
double result = curr * result2;
etResult.setText(String.valueOf(result));
}
});
public int calculateCurrency()
{
int[] currency = getResources().getIntArray(R.array.currenciesvalues);
return Integer.parseInt((String) spinner1.getSelectedItem()) / Integer.parseInt((String)spinner2.getSelectedItem());//currenciesvalues[spinner2.getSelectedItemPosition()];
}
Button homeBtn = (Button) findViewById(R.id.btn_currencyhome);
homeBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(CurrencyActivity.this, MainActivity.class));
}
});
}
}