货币转换器无法在Android中运行

时间:2013-06-16 16:24:22

标签: android

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

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

}

吐司部分正在运行,但货币转换不是(请查看onClick功能)。我有两个旋转器。要么吐司正在显示,要么当我点击计算时,应用程序正在崩溃。

1 个答案:

答案 0 :(得分:2)

edittexteuros似乎没有被初始化。

onCreate()中,请确保按照以下方式对其进行初始化:

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

否则当您在setText中尝试使用convertDollarstoEuros()时,您将获得NullPointerException

当然,所有其他View元素也是如此。您已为spinner1spinner2calculate完成了此操作,但尚未设置其他所有内容。