0

TextView計算結果をまたはに表示しようとしていますEditText。one100lbsとtenPoundsからユーザー入力を取得し、それを合計して、totalPoundsに表示しようとしています。これは私が使用する方程式ではありませんが、それが機能することを確認したいだけです。現在、以下のコードでアプリケーションがクラッシュします。これはすべて1つ未満activityです。また、変更EditTextの場所のIDを変更するとどうなりますか?リンクを付けないでください、私はその単純さを知っていますが、私は初心者です。私は検索しましたが、解決策を見つけるのに苦労しています。editTextrelative layout

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.pounds);
    addListenerOnSpinnerItemSelection();

    EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
    int one = Integer.valueOf(one100lbs.getText().toString());

    EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
    int two = Integer.valueOf(tenPounds.getText().toString());

    int result = one + two;

    TextView textView = (TextView) findViewById(R.id.totalPounds);
    textView.setText(result);   
}
4

2 に答える 2

4

あなたは次のようなものが欲しいです:

textView.setText(String.valueOf(result));

現状では、intのみを指定すると、AndroidはリソースIDを見つけようとしますが、失敗します。

また、数字を入力するときに失敗することで有名なEditTextsを使用していることもあります。これは、キーパッドを数字のみにするだけでなく、次のようなこともできます。

int one = 0;
int two = 0;

try{
  EditText one100lbs = (EditText) findViewById(R.id.one100lbs);
  one = Integer.valueOf(one100lbs.getText().toString().trim());
}
catch (NumberFormatException e)
{
  one = -1;
}

try{
  EditText tenPounds = (EditText) findViewById(R.id.tenPounds);
  two = Integer.valueOf(tenPounds.getText().toString().trim()); 
}
catch (NumberFormatException e)
{
  two = -1;
}

int result = one + two;

TextView textView = (TextView) findViewById(R.id.totalPounds);
textView.setText(String.valueOf(result)); 
于 2013-01-14T23:38:31.597 に答える
0

次のいずれかを使用できます。

textView.setText(Integer.toString(result));

また

textView.setText(result + "");
于 2013-01-15T00:18:50.983 に答える