java.lang.NumberFormatException是否有更好的解决此问题的方法?

时间:2013-07-24 17:36:07

标签: android numberformatexception

这是我正在处理的应用程序的Java文件。在logcat中,它说这行中的initvar()方法有一个错误“length1et =(EditText)findViewById(R.id.etlength1);”我知道这是因为在edittext框中没有变量,我之前遇到过这个问题,只是把0放在那里开始。是否有一个简单的命令,在那里什么也没有,它只会理解自动为值添加0或是写一些代码将其视为零的唯一方法?如果有必要,java代码下面是xml。在之前的应用程序中,我刚刚读过edittext框,如果没有,请在执行任何其他操作之前将值设置为0。为了记录,我知道我没有按钮来调用calculate()方法,我还没有把它放进去。

package com.example.constructioncalculator;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class Paint extends Activity {

    int numwin, numdoor;
    double areadoor, areawin, lengthft, lengthin, widthft, widthin, heightft,
            heightin, areawall, areaceil, paintcon, paintneeded;
    EditText length1et, length2et, width1et, width2et, height1et, height2et,
            paintconet, paintneededet;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.paintdisp);

        initvar();
        calculate();
        output();

    }

    private void initvar() {
        // TODO Auto-generated method stub
        numwin = 0;
        numdoor = 0;
        areawin = 20;
        areadoor = 19.5;
        lengthft = 0;
        lengthin = 0;
        widthft = 0;
        widthin = 0;
        heightft = 0;
        heightin = 0;
        areawall = 0;
        areaceil = 0;
        paintcon = 0;
        paintneeded = 0;

        length1et = (EditText) findViewById(R.id.etlength1);
        length2et = (EditText) findViewById(R.id.etlength2);
        width1et = (EditText) findViewById(R.id.etwidth1);
        width2et = (EditText) findViewById(R.id.etwidth2);
        height1et = (EditText) findViewById(R.id.etheight1);
        height2et = (EditText) findViewById(R.id.etheight2);
        paintconet = (EditText) findViewById(R.id.etpaintcon);
        paintneededet = (EditText) findViewById(R.id.etpaintneeded);



    }

    private void calculate() {
        // TODO Auto-generated method stub

    lengthft = Double.parseDouble(length1et.getText().toString());  
        lengthin = Double.parseDouble(length2et.getText().toString());
        widthft = Double.parseDouble(width1et.getText().toString());
        widthin = Double.parseDouble(width2et.getText().toString());
        heightft = Double.parseDouble(height1et.getText().toString());
        heightin = Double.parseDouble(height2et.getText().toString());
        paintcon = Double.parseDouble(paintconet.getText().toString());

        areaceil = (lengthft + lengthin / 12) * (widthft + widthin / 12);
        areawall = areaceil * (heightft + heightin / 12) - (numwin * areawin)
                - (numdoor * areadoor);
        paintneeded = (areawall / paintcon) * 1.1;

    }

    private void output() {
        // TODO Auto-generated method stub
        paintneededet.setText(String.valueOf(paintneeded));
    }

}

** * ** * ** *这是我的xml文件......

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/TextView04"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Paint Coverage Estimator"
                android:textSize="30dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/editText1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:ems="10"
            android:text="@string/paintinst" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvlength"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Length"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/tvwidth"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Width"
                android:textSize="20dp" />

            <TextView
                android:id="@+id/tvheight"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:text="Height"
                android:textSize="20dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <EditText
                android:id="@+id/etlength1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="feet"
                android:text="1"
                android:textSize="10dp" />

            <EditText
                android:id="@+id/etlength2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="inches"
                android:text="1"
                android:textSize="10dp" />

            <EditText
                android:id="@+id/etwidth1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="feet"
                android:text="1"
                android:textSize="10dp" />

            <EditText
                android:id="@+id/etwidth2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="inches"
                android:text="1"
                android:textSize="10dp" />

            <EditText
                android:id="@+id/etheight1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="feet"
                android:text="1"
                android:textSize="10dp" />

            <EditText
                android:id="@+id/etheight2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:gravity="center"
                android:hint="inches"
                android:text="1"
                android:textSize="10dp" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/editText2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="approx paint coverage (ft^2)?" />

            <EditText
                android:id="@+id/etpaintcon"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="300" >

                <requestFocus />
            </EditText>

        </LinearLayout>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/editText3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="Paint needed of your buckets" />

            <EditText
                android:id="@+id/etpaintneeded"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="" />

        </LinearLayout>

    </LinearLayout>

</LinearLayout>

1 个答案:

答案 0 :(得分:1)

试试这个:

lengthft = Double.parseDouble(length1et.getText().toString()+0); 

但请注意,如果用户输入的数字无效并点击了尚未实施的按钮,您的应用就会崩溃。因此,在调用calculate()之前,您应该在所有EditText中添加一个检查。

或者甚至更好,添加一个功能来为您完成:

lengthft = getDouble(length1et); 



public double getDouble(EditText et) {

    if (!et.getText().equals(null)) {
        return Double.parseDouble(et.getText().toString()); //convert your string into integer 
    } else {
        return 0; // or what you want to return if string is Null
    }
}