Android文本输入总是给我NaN值

时间:2017-08-18 09:17:23

标签: java android nan

我无法弄清楚为什么此代码输出始终显示NaN。有没有人可以帮我弄清楚我错在哪里?

package com.example.user.solar_calculator;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;

import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;

public class efficiency extends Activity {
    Button button;
    EditText efficiency;
    EditText vol;

    String e,v;

    double e1,wat,v1, AC_Load_in_ampere_hour_calculation,dc;
    double load,load_vol,Total_Daily_load,lol;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_efficiency);
        initControls();
        addListenerOnButton();
    }

    public void initControls() {
        efficiency=(EditText)findViewById(R.id.editText1);
        vol=(EditText)findViewById(R.id.editText2);
        button=(Button)findViewById(R.id.button);
        button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    e1=Double.parseDouble(efficiency.getText().toString()); // Make use of autoboxing.  It's also easier to read.
                } catch (NumberFormatException e) {
                    e1=1;
                }
                try {
                    v1=Double.parseDouble(vol.getText().toString());// Make use of autoboxing.  It's also easier to read.
                } catch (NumberFormatException e) {
                    v1=1;
                }

                Bundle extras = getIntent().getExtras();
                if(extras !=null) {
                    String value = extras.getString("FOUR");
                    load=Double.parseDouble(value.toString());
                }
                wat=load_vol*(1+.1);

                AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));

                Total_Daily_load=AC_Load_in_ampere_hour_calculation+dc;

                Intent i = new Intent(efficiency.this, calculation.class);
                //Create the bundle
                Bundle bundle = new Bundle();
                //Add your data to bundle

                bundle.putString("seven", String.valueOf(AC_Load_in_ampere_hour_calculation));
                bundle.putString("eight", String.valueOf(Total_Daily_load));
                bundle.putString("nine", String.valueOf(dc));

                //Add the bundle to the intent
                i.putExtras(bundle);
                //Fire that second activity
                startActivity(i);
            }
        });
    }

    public void addListenerOnButton() {    
        final Context context = this;    
        button = (Button) findViewById(R.id.button);    
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

1 个答案:

答案 0 :(得分:2)

我认为造成问题的行是AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));

因为如果v1 = 0则不可能。