0

私が作成した非常に単純なコンバーターアプリがあります。データがいずれかのテキストボックスに入力されている場合は正常に動作しますが、両方を空白のままにしていずれかの変換ボタンをクリックするとクラッシュするため、これを処理する if ステートメントを使用しましたが、そのステートメントを無視してとにかくクラッシュしたようです。基本的に、ボタンをクリックして数字が入力されていない場合にやりたいことは、ユーザーにデータを入力するように求めるメッセージを画面にトーストするだけです。

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    final EditText txtCentimetersValue = (EditText) findViewById(R.id.txtCentimetersValue);
    final EditText txtInchesValue = (EditText) findViewById(R.id.txtInchesValue);

    Button btnConvert = (Button) findViewById(R.id.btnConvert);
    Button btnConvert2 = (Button) findViewById(R.id.btnConvert2);
    Button btnClear = (Button) findViewById(R.id.btnClear);

    btnClear.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {
            txtCentimetersValue.setText("");
            txtInchesValue.setText("");
        }
    });

    btnConvert.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0)
        {

                txtInchesValue.setText("");

                double centimeters = Double.valueOf(txtCentimetersValue.getText().toString());
                double inches = centimeters / 0.393700787;

                txtInchesValue.setText(String.valueOf(inches));
        }


    });


    btnConvert2.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View arg0)
        {

                txtCentimetersValue.setText("");

                double inches = Double.valueOf(txtInchesValue.getText().toString());
                double centimeters = inches / 0.393700787;

                txtCentimetersValue.setText(String.valueOf(centimeters));
        }

    });
}

}

4

1 に答える 1

1

これを処理するために if ステートメントを含めようとしましたが、そのステートメントを無視してとにかくクラッシュしたようです。

if ステートメントが表示されません。おそらく、==文字列を比較するために使用しようとしていたか (Java では実行できません)、値がnull. とにかく、単に使用できますisEmpty()

String value = txtCentimetersValue.getText().toString();
if(!value.isEmpty()) {
    double centimeters = Double.valueOf(value);
    double inches = centimeters / 0.393700787;

    txtInchesValue.setText(String.valueOf(inches));
}

インチからセンチメートルについても同様のことを行います。

于 2013-04-14T00:23:09.170 に答える