0

これが、私が取り組んでいるアプリの Java ファイルです。logcat では、この行の initvar() メソッドにエラーがあると言っています "length1et = (EditText) findViewById(R.id.etlength1);" これは、edittext ボックスに変数がなく、以前にこの問題が発生したことがあり、そこに 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 に答える