1

編集テキスト値をフロートに割り当てようとすると、アプリが強制終了します。これがコードです。Float.valueOf() の代わりに Float.parseFloat() メソッドも試しました。getETvalue() メソッドを外すと、アプリは機能します。助けてください

package com.example.aviator_18;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    float Remaining,Departure,TotUplift,SG,DiscResult;
    int CalUpliftResult;
    TextView RemainingTV,DepartureTV,UpliftTV,SGtv,CalcUpliftTV,DiscrepancyTV;
    EditText RemainingET,DepartureET,TotUpliftET,SGet,CalcUpliftET,DiscrepancyET;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setupView();
        getETvalue();
        evaluation();
        CalcUpliftTV.setText(String.valueOf(CalUpliftResult));
        DiscrepancyTV.setText(String.valueOf(DiscResult));


    }

    private void getETvalue() {
        String a,b,c,d,e;
        Remaining=Float.valueOf(RemainingET.getText().toString());
        Departure=Float.valueOf(DepartureET.getText().toString());
        TotUplift=Float.valueOf(TotUpliftET.getText().toString());
        SG=Float.valueOf(SGet.getText().toString());



    }

    private void evaluation() {


        CalUpliftResult=Math.round(TotUplift*SG);
        DiscResult=((Departure-Remaining-CalUpliftResult)/CalUpliftResult)*100;

    }

    private void setupView() {
        RemainingET=(EditText)findViewById(R.id.remainingET);
        DepartureET=(EditText)findViewById(R.id.departureET);
        TotUpliftET=(EditText)findViewById(R.id.TotalUpliftET);
        SGet=(EditText)findViewById(R.id.SGet);

        CalcUpliftTV=(TextView)findViewById(R.id.CalUpliftResult);
        DiscrepancyTV=(TextView)findViewById(R.id.discResult);



    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

}
4

2 に答える 2

0

文字列を float に解析できることを確認する必要があります。try - catch を追加できます

try {
   Remaining=Float.parseFloat(RemainingET.getText().toString());
} catch (NumberFormatException nfe) {
    // Handle exception
}
于 2013-02-17T12:28:40.440 に答える
0

Float.valueOf文字列形式の浮動小数点数を含む文字列値を変換します。ただし、文字列にフロートに解析できないものが含まれている場合、このメソッドはスローしますNumberFormatException

お気に入り:

float f = Float.valueOf("12.35") //this is correct

その間

float f = Float.valueOf("abcd") //this is incorrect

投げる必要があるため、強制的に閉じますNumberFormatException

Javadoc から

値の

public static Float valueOf(String s)
                     throws NumberFormatException
Returns a Float object holding the float value represented by the argument string s.

Throws:
NumberFormatException - if the string does not contain a parsable number.
于 2013-02-17T12:24:16.363 に答える