0

这是我正在处理的应用程序的 Java 文件。在 logcat 中它说 initvar() 方法在这一行“length1et = (EditText) findViewById(R.id.etlength1);”中有一个错误 我知道这是因为编辑文本框中没有变量,而我之前也遇到过这个问题,只需将 0 放在那里即可。是否有一个简单的命令在那里什么都没有,它只会理解自动输入一个 0 作为值,或者是编写一些代码将其视为零的唯一方法?如果有必要,Java 代码下方是 xml。在以前的应用程序中,我刚刚让它读取了编辑文本框,如果那里什么都没有,在它执行任何其他操作之前将值设置为 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>
4

1 回答 1

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
    }
}
于 2013-07-24T19:26:24.687 回答