应用关闭按钮点击

时间:2017-06-05 09:33:30

标签: android buttonclick

我的应用正在尝试进行一些计算并检查条件并打印满足条件的数字数组。但应用程序正在关闭点击。请帮助..

package com.example.ganesha.app1;

import android.support.v7.app.AppCompatActivity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import static android.R.attr.button;

public class MainActivity extends AppCompatActivity {
    private Button result;
    private EditText cap,pwfac,dis,solution;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        result = (Button) findViewById(R.id.resbut);
        cap = (EditText) findViewById(R.id.capacity);
        pwfac = (EditText) findViewById(R.id.powerfactor);
        dis = (EditText) findViewById(R.id.distance);
        solution = (EditText) findViewById(R.id.result);
        //mytextview = (TextView)findViewById(R.id.result)

        result.setOnClickListener(new View.OnClickListener() {

          @Override
          public void onClick(View view) {
            Double capacity = Double.valueOf(cap.getText().toString());
            Double powerfactor = Double.valueOf(pwfac.getText().toString());
            Double distance = Double.valueOf(dis.getText().toString());
            Double amps = capacity / (718.80 * powerfactor);
            Double minimum = 24.9;
            Double ampspermtr=amps/distance;
            Double[] cases = {26.8156, 16.4013, 10.2083, 6.8180, 4.0509, 2.5470, 1.6151, 1.1689, 0.8673, 0.6075, 0.4458, 0.3616, 0.3028, 0.2532, 0.2082, 0.1812, 0.1604, 0.1461, 0.1359};
            Double [] volts=new Double[19];
            for(int i = 0; i <19; i++)
            {
                volts[i]=cases[i]*ampspermtr;
            }

            Double [] wires={1.50,2.50,4.0,6.0,10.0,16.0,25.0,35.0,50.0,70.0,95.0,120.0,150.0,185.0,240.0,300.0,400.0,500.0,630.0};
            final Double [] allpossible = new Double[19];
            int c=0;
            for(int j=0;j<19;j++)
            {
                if(volts[j]<minimum)
                {
                    allpossible[c]=wires[j];

                }
                c++;
            }
            final int g= c;

            for(int p=0;p<g;p++)
            {
               String value = Double.toString(allpossible[g]);
               solution.append(value);
            }
        }
    });

它基于电气工程。可以找到错误并帮助我

1 个答案:

答案 0 :(得分:1)

  

实际上你在计算中发生了一些异常   OnClick Button您应该编写计算代码环绕声   与try catch区块一样,如下面

try{
Double capacity = Double.valueOf(cap.getText().toString());
            Double powerfactor = Double.valueOf(pwfac.getText().toString());
            Double distance = Double.valueOf(dis.getText().toString());
            Double amps = capacity / (718.80 * powerfactor);
            Double minimum = 24.9;
            Double ampspermtr=amps/distance;
            Double[] cases = {26.8156, 16.4013, 10.2083, 6.8180, 4.0509, 2.5470, 1.6151, 1.1689, 0.8673, 0.6075, 0.4458, 0.3616, 0.3028, 0.2532, 0.2082, 0.1812, 0.1604, 0.1461, 0.1359};
            Double [] volts=new Double[19];
            for(int i = 0; i <19; i++)
            {
                volts[i]=cases[i]*ampspermtr;

            }
            Double [] wires={1.50,2.50,4.0,6.0,10.0,16.0,25.0,35.0,50.0,70.0,95.0,120.0,150.0,185.0,240.0,300.0,400.0,500.0,630.0};
            final Double [] allpossible = new Double[19];
            int c=0;
            for(int j=0;j<19;j++)
            {
                if(volts[j]<minimum)
                {
                    allpossible[c]=wires[j];

                }
                c++;
            }
            final int g= c;

            for(int p=0;p<g;p++)
         {

             String value = Double.toString(allpossible[g]);
             solution.append(value);
         }
        }

}Catch(Exception ex){
// handle your Exception here
}