Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/210.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
JavaEclipse中的按钮_Java_Android - Fatal编程技术网

JavaEclipse中的按钮

JavaEclipse中的按钮,java,android,Java,Android,我做了一个计算几何平均值的程序,我的程序已经在运行了,但是我有一些错误。每当我在inputValues中单击带有空输入的BTN计算时,我的程序就会停止工作。我将如何处理这个错误?谢谢:) final AutoCompleteTextView inputValues=(AutoCompleteTextView)findViewById(R.id.txt\u输入); 最终文本视图txtTotalNum=(文本视图)findViewById(R.id.txt_totalNumber); 最终TextV

我做了一个计算几何平均值的程序,我的程序已经在运行了,但是我有一些错误。每当我在inputValues中单击带有空输入的BTN计算时,我的程序就会停止工作。我将如何处理这个错误?谢谢:)

final AutoCompleteTextView inputValues=(AutoCompleteTextView)findViewById(R.id.txt\u输入);
最终文本视图txtTotalNum=(文本视图)findViewById(R.id.txt_totalNumber);
最终TextView GeoMean=(TextView)findViewById(R.id.txt_GeoMean);
按钮btnCalculate=(按钮)findViewById(R.id.btnCalculate);
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图agr0){
字符串[]值=(inputValues.getText().toString().split(“,”);//这是使用split方法在editText中输入的值
txtTotalNum.setText(Integer.toString(values.length));//计算输入的数量
double[]convertedValues=新的double[values.length];
双产品1=1.0;
双积=1.0;
对于(int a=0;a
@覆盖
公共void onClick(视图agr0){
字符串[]值=(inputValues.getText().toString().split(“,”);//这是使用split方法在editText中输入的值
如果(值!=NULL){
txtTotalNum.setText(Integer.toString(values.length));//计算输入的数量
double[]convertedValues=新的double[values.length];
双产品1=1.0;
双积=1.0;
对于(int a=0;a
//试试这个
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图agr0){
if(inputValues.getText().toString().length()>0){
字符串[]值=(inputValues.getText().toString().split(“,”);//这是使用split方法在editText中输入的值
txtTotalNum.setText(Integer.toString(values.length));//计算输入的数量
double[]convertedValues=新的double[values.length];
双产品1=1.0;
双积=1.0;
对于(int a=0;a
您的程序在

 convertedValues[a] =Integer.parseInt(values[a]);
因为对于空输入,拆分数组没有值,因此其长度为0。当您尝试访问
值[0]
时,会出现ArrayIndexOutOfBoundsException

在拆分之前,只需检查输入是否为emty

if(!inputValues.getText().toString().equals("")){
    ... // do your stuff
} else {
  // show a message
}

出现错误是因为您的代码试图拆分空字符串或null,然后访问实际上根本没有索引的值[]。只需检查EditText获取的字符串的长度是否大于0即可

@Override
    public void onClick(View agr0) {
        String fetchedValues = ( inputValues.getText().toString());
        if(fetchedValues.length>0)
        {
          String []values = fetchedValues.split(",");
          txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
         double[] convertedValues = new double[values.length];

         double product1 =1.0;
         double product=1.0;

         for(int a = 0; a < convertedValues.length; a++){
            convertedValues[a] =Integer.parseInt(values[a]);
            //product *=convertedValues[a];
         }

         double geoMean = Math.pow(product, product1/convertedValues.length);
          GeoMean.setText(Double.toString(geoMean));
       }
       else
       {
          //alert the user that the field is empty
        }
    }
@覆盖
公共void onClick(视图agr0){
字符串fetchedValues=(inputValues.getText().toString());
if(fetchedValues.length>0)
{
String[]values=fetchedValues.split(“,”);
txtTotalNum.setText(Integer.toString(values.length));//计算输入的数量
double[]convertedValues=新的double[values.length];
双产品1=1.0;
双积=1.0;
对于(int a=0;a
您遇到了什么错误。发布日志。您的意思是您收到了“应用意外停止…”在Android屏幕上?那么你应该在Logcat中有堆栈跟踪。它在说什么?你能告诉我们吗?主要是空指针异常。检查值是否为非空..或者在你的onClick中检查Logcat错误是否为空。如果不是空,则执行,否则跳过该块
 convertedValues[a] =Integer.parseInt(values[a]);
if(!inputValues.getText().toString().equals("")){
    ... // do your stuff
} else {
  // show a message
}
@Override
    public void onClick(View agr0) {
        String fetchedValues = ( inputValues.getText().toString());
        if(fetchedValues.length>0)
        {
          String []values = fetchedValues.split(",");
          txtTotalNum.setText(Integer.toString(values.length));//calculate the number of inputs
         double[] convertedValues = new double[values.length];

         double product1 =1.0;
         double product=1.0;

         for(int a = 0; a < convertedValues.length; a++){
            convertedValues[a] =Integer.parseInt(values[a]);
            //product *=convertedValues[a];
         }

         double geoMean = Math.pow(product, product1/convertedValues.length);
          GeoMean.setText(Double.toString(geoMean));
       }
       else
       {
          //alert the user that the field is empty
        }
    }