-1

我写了这个方法,但不断收到错误消息:

令牌“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));         
        }     
    });
}

}

4

1 回答 1

0

由于代码格式错误,很难说,但我怀疑这句话是问题所在(就在您最初发布的代码之上):

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

该代码根本不属于方法或构造函数——它只是在类的中间。唯一可以直接存在于类中的是字段声明、方法、构造函数、析构函数、嵌套类和初始化程序。

我怀疑您实际上打算将其放入构造函数中。最后的代码与homeBtn.

因此,作为一个较短的示例,此代码无效:

public class InvalidCode {
    private Button button = new Button();
    button.setText("Foo");
}

但这是有效的:

public class ValidCode {
    private Button button = new Button();

    public Button() {
        button.setText("Foo");
    }
}
于 2013-07-26T18:13:59.300 回答