0

私はAndroidプログラミングが初めてです。次のプログラムは、単純な華氏から摂氏への変換を行います。華氏の EditText に値を入力すると、すぐに摂氏に変換されます。およびその逆。

次のエラーが表示されます。

華氏のテキスト ボックスに値を入力し続けても問題はありません。値も削除すると、最後の文字までうまく削除されています。(エミュレータで)バックスペースを押して最後の文字を削除すると、実行が停止します。コードを実行すると、次のエラーが発生します。

2) フォーカスが摂氏の EditText に設定されていても、エミュレーターは開始時にフォーカスされているものとして常に華氏を表示します。

3) 摂氏の編集テキストで行った変更が華氏に反映されません。

(デバッグせずにフォーラムに投稿したと思わないでください。このフォーラムをクリーンに保つために、ここに投稿する前に 3 時間以上試しました )

07-29 01:59:21.189: E/AndroidRuntime(1390): java.lang.NumberFormatException: 無効なフロート: ""

以下は私のMainActivity.Javaです

  public class MainActivity extends Activity {
     private EditText celsiusText;
     private EditText farenheitText;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        celsiusText = (EditText) findViewById(R.id.editText1);
        farenheitText = (EditText) findViewById(R.id.editText2);
        celsiusText.requestFocus();
        celsiusText.setOnFocusChangeListener((OnFocusChangeListener) this);
        farenheitText.setOnFocusChangeListener((OnFocusChangeListener) this);
    }
       public void onFocusChange(View v, boolean hasFocus)

        {

           TextWatcher watcher1 = new TextWatcher(){
            public void afterTextChanged(Editable s) {

               }

               public void beforeTextChanged(CharSequence s, int start, 
                 int count, int after) {
               }
            public void onTextChanged(CharSequence S,int start,int before, int count){
                float inputValue;
                if (!S.toString().equals(""))
                {
                    inputValue = Float.parseFloat(S.toString());
                             ((EditText)findViewById(R.id.editText1)).setText(String
                                    .valueOf(convertFahrenheitToCelsius(inputValue)));

                }
                else
                {
                     ((EditText)findViewById(R.id.editText1)).setText("");
                    return;
                }
                        }
           };



           TextWatcher watcher2 = new TextWatcher()
           {
                        public void afterTextChanged(Editable s) {

                   }

                   public void beforeTextChanged(CharSequence s, int start, 
                     int count, int after) {
                   }
                public void onTextChanged(CharSequence S,int start,int before, int count){
                    float inputValue;
                    if (!S.toString().equals(""))
                    {
                        inputValue = Float.parseFloat(S.toString());
                     ((EditText)findViewById(R.id.editText2)).setText(String
                                .valueOf(convertCelsiusToFahrenheit(inputValue)));

                    }
                    else
                    {
                        ((EditText)findViewById(R.id.editText2)).setText("");
                        return;
                    }
                            }
                };

                if((v == findViewById(R.id.editText2)) &&  (hasFocus==true)) 
                {
                farenheitText.addTextChangedListener(watcher1);
                }
                else if ((v == findViewById(R.id.editText1)) &&  (hasFocus==true)) 
                {
                    ((EditText)findViewById(R.id.editText1)).setText("");
                celsiusText.addTextChangedListener(watcher2);
                }
        }





//Converts to celsius
      private float convertFahrenheitToCelsius(float fahrenheit) {
        return ((fahrenheit - 32) * 5 / 9);
      }

      // Converts to fahrenheit
      private float convertCelsiusToFahrenheit(float celsius) {
        return ((celsius * 9) / 5) + 32;
      }

}

私のActivity_main.xmlは

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="16dp"
    android:paddingRight="16dp" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="62dp"
        android:ems="10"
        android:inputType="numberSigned" >

        <requestFocus />
    </EditText>

    <EditText
        android:id="@+id/editText2"
        android:layout_width="128dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/editText1"
        android:layout_alignBottom="@+id/editText1"
        android:layout_alignParentRight="true"
        android:ems="10"
        android:inputType="numberSigned" />

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="18dp"
        android:text="@string/celsius"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/textView1"
        android:layout_alignBottom="@+id/textView1"
        android:layout_alignRight="@+id/editText2"
        android:text="@string/fahrenheit"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

ご覧のとおり、次のステップは両側のテキスト ボックスを編集することです。このコードをセルシウスに変換する方法 (エラーがスローされないように setfocus を使用) のヘルプもいただければ幸いです。

4

3 に答える 3

3

問題はonTextChanged()-method にあります:

float inputValue;
if (S != "") {
    inputValue = Float.parseFloat(S.toString());
    // ...
}

チェックする文字列オブジェクトが同じではないため、if は決して真になりません。正しい方法は次のとおりです。

if (!S.toString().equals(""))

または、さらに良い:

if (S.length > 0)

これにより、Float.parseFloat(String)-method が空の文字列で呼び出され、NumberFormatException.

于 2012-07-29T12:59:24.157 に答える
0

あなたのコードは NULL 値を摂氏に変換しようとしています。

if(inputValue==null){
// Handle the situation here
}
于 2012-07-29T12:57:29.733 に答える
0

文字列の長さも確認してみてください。

if (S != "" && S.length() > 0){
                    inputValue = Float.parseFloat(S.toString());
                    celsiusText.setText(String
                            .valueOf(convertFahrenheitToCelsius(inputValue)));
                }
于 2012-07-29T12:59:32.803 に答える