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
Android 输入点(.)时,计算器崩溃_Android - Fatal编程技术网

Android 输入点(.)时,计算器崩溃

Android 输入点(.)时,计算器崩溃,android,Android,我有以下代码来计算在3个编辑文本中的2个文本中输入的值 public void calculeaza() { totaltest = 0; prod = new String[allprod.size()]; pret = new String[allpret.size()]; cant = new String[allcant.size()]; for (int m = 0; m < allprod.size(); m++) {

我有以下代码来计算在3个编辑文本中的2个文本中输入的值

public void calculeaza() {

    totaltest = 0;
    prod = new String[allprod.size()];
    pret = new String[allpret.size()];
    cant = new String[allcant.size()];

    for (int m = 0; m < allprod.size(); m++) {

        prod[m] = allprod.get(m).getText().toString();
        if (prod[m].matches("")) {
            prod[m] = Float.toString(0);

        }
    }

    for (int j = 0; j < allcant.size(); j++) {

        cant[j] = allcant.get(j).getText().toString();
        if (cant[j].matches("")) {
            cant[j] = Float.toString(0);

        }
    }

    for (int k = 0; k < allpret.size(); k++) {
        pret[k] = allpret.get(k).getText().toString();
        if (pret[k].matches("")) {
            pret[k] = Float.toString(0);

        }
    }

    for (int l = 0; l < allpret.size(); l++) {

        Float temp = Float.parseFloat(cant[l]) * Float.parseFloat(pret[l]);

        totaltest = totaltest + temp;

        TextView totalf = (TextView) findViewById(R.id.total);
        totalf.setText(String.format("Total: %.2f", totaltest));

    }
}
签出
TextView
的字段。我想如果您将它设置为
numberDecimal
,它的格式总是正确的

布局:

EditText android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
代码:


使用editText上的TextWatcher查找“.”,将字符串相应地转换为十进制分数并将其转换为双精度点。是否使用最终结果进行计算。

请发布崩溃的堆栈跟踪。是什么阻止您在for循环中进行检查<代码>if(pret[k].matches(“”){pret[k]=Float.toString(0);}否则if(pret[k].charAt(0)='.'){pret[k]=“0”+allpret.get(k).getText().toString();}非常感谢Luksprog,非常棒!我不知道。查拉特,我每天都学到新东西!谢谢我把它作为输入类型:et2.setInputType(InputType.type_CLASS_NUMBER | InputType.type_NUMBER_FLAG_DECIMAL);你不可能知道,但是我的编辑文本是动态创建的,所以它们不在任何XML中。无论如何,谢谢你,我已经修复了它。
EditText android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal" />
EditText ed = (EditText) findViewById(R.id.textView);
String s = ed.getText().toString();
float f = Float.valueOf(s);

Log.e(TAG, "Value: " + f);