以下是我正在處理的應用程序的Java文件。在logcat中,它說這行中的initvar()方法有錯誤「length1et =(EditText)findViewById(R.id.etlength1);」我知道這是因爲在edittext框中沒有變量,我之前有過這個問題,只是把0放在那裏開始。是否有一個簡單的命令在那裏沒有任何東西,它只會理解自動將值設置爲0,或者是編寫一些代碼將其視爲零的唯一方法?如果需要,java代碼下面是xml。在以前的應用程序中,我剛剛閱讀了edittext框,如果沒有任何內容,請在其它任何操作之前將該值設置爲0。對於記錄我知道我沒有一個按鈕來調用calculate()方法,我還沒有把它放入。java.lang.NumberFormatException有沒有更好的解決此問題的方法?
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>
如果這取決於用戶的輸入,那麼你爲什麼不限制或驗證輸入? – NINCOMPOOP
該行沒有任何問題。你得到的錯誤是什麼?你應該可以在沒有拋出錯誤的情況下得到一個空的EditText,這使我相信真正的錯誤是在別處。 – Samuel
你正試圖解析一個空字符串'「」'。從你的onCreate()中,你可以調用'initvar()'和'calculate()'。但是,'EditTexts'沒有任何文本,因此'NumberFormatException'。 – Vikram