0

我有一个转换货币的安卓程序。问题是货币没有转换。这是我的代码。

package com.example.currencyconverter;

import java.text.DecimalFormat;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity 
                          implements OnClickListener, 
                          OnItemSelectedListener{

    EditText edittextdollars;
    EditText edittexteuros;
    TextView dollars;
    TextView euros;
    RadioButton dtoe;
    RadioButton etod;
    RadioGroup radiogroup;
    Spinner spinner1;
    Spinner spinner2;
    Button calculate;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        addListenerOnButton();
    }

    private void addListenerOnButton() {
        spinner1 = (Spinner) findViewById(R.id.spinner1);
        spinner2 = (Spinner) findViewById(R.id.spinner2);
        calculate = (Button) findViewById(R.id.calculate);
    }
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {    
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {
        // TODO Auto-generated method stub
    }


    @Override
    public void onClick(View v) {
        String text = spinner1.getSelectedItem().toString().trim();
        String text2 = spinner2.getSelectedItem().toString().trim();
        if (text.equalsIgnoreCase("Dollar") && text2.equalsIgnoreCase("Euro")) {
            convertDollarstoEuros();
        }
        Toast.makeText(MainActivity.this,
            "OnClickListener : " + 
            "\nSpinner 1 : "+ String.valueOf(spinner1.getSelectedItem()) + 
            "\nSpinner 2 : "+ String.valueOf(spinner2.getSelectedItem()),
        Toast.LENGTH_SHORT).show();
    }

    public void convertDollarstoEuros() {

        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat("###.##");
        edittexteuros.setText(df.format(val*0.77));
        }

    protected void convertEurostoDollars() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat("###.##");
        edittextdollars.setText(df.format(val*1.3));

        }

    protected void convertEurostoEuros() {
        double val = Double.parseDouble(edittexteuros.getText().toString());
        DecimalFormat df = new DecimalFormat("###.##");
        edittextdollars.setText(df.format(val*1));
    }

    protected void convertDollarstoDollars() {
        double val = Double.parseDouble(edittextdollars.getText().toString());
        DecimalFormat df = new DecimalFormat("###.##");
        edittexteuros.setText(df.format(val*1));
    }

}

toast 部分正在工作,但货币转换不起作用(请查看 onClick 函数)。我有两个旋转器。吐司只是显示,或者当我单击计算时,应用程序崩溃了。

4

1 回答 1

2

edittexteuros似乎没有初始化。

在你onCreate()确保你像这样初始化它:

edittexteuros = (EditText) findViewById(R.id.name_of_your_edit_text_id_in_xml);

否则,当您尝试setText使用它时,convertDollarstoEuros()您会得到一个NullPointerException.

当然,所有其他 View 元素也是如此。您已经为 , 完成了它spinner1spinner2但是calculate所有其他的都没有设置。

于 2013-06-16T16:27:47.283 回答