2012-03-05 5 views
0

#を押したときに2 + 3 =のようなランダム生成式を表示したい。私はこのコードを実装しましたが、それを押すとアプリケーションがクラッシュします。#ボタンをクリックしたときのテキストの表示

私のXMLコードは次のとおりです。

<TextView 
     android:id="@+id/randomNumberGen" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:textSize="45dp" /> 

Javaコードです:

package org.example.question; 

import java.util.Random; 


import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class QuestionActivity extends Activity implements View.OnClickListener { 
    /** Called when the activity is first created. */ 
    int fnum, snum; 


Button one,two,three,four,five,six,seven,eight,nine,zero,minus,hash; 
    TextView display; 

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

     final Random myRandom = new Random(); 

     display = (TextView)findViewById(R.id.randonNumberGen); 

     //Buttons 
     one= (Button) findViewById(R.id.keypad_1); 
     two= (Button) findViewById(R.id.keypad_2); 
     three= (Button) findViewById(R.id.keypad_3); 
     four= (Button) findViewById(R.id.keypad_4); 
     five = (Button) findViewById(R.id.keypad_5); 
     six= (Button) findViewById(R.id.keypad_6); 
     seven = (Button) findViewById(R.id.keypad_7); 
     eight = (Button) findViewById(R.id.keypad_8); 
     nine = (Button) findViewById(R.id.keypad_9); 
     minus = (Button) findViewById(R.id.keypad_subtract); 
     hash = (Button) findViewById(R.id.keypad_hash); 


     one.setOnClickListener(this);  two.setOnClickListener(this);  three.setOnClickListener(this); 
     three.setOnClickListener(this); four.setOnClickListener(this);  five.setOnClickListener(this); 
     six.setOnClickListener(this);  seven.setOnClickListener(this);  eight.setOnClickListener(this); 
     nine.setOnClickListener(this);  minus.setOnClickListener(this);  hash.setOnClickListener(this); 

} 

    public void onClick(View arg0) { 
     View v = null; 
     switch(v.getId()){ 
     case R.id.keypad_hash: 
      display.setText(fnum+"+"+ snum+"="); 
      fnum = (int) ((double) ((Math.random() * 1000))/100.0); 
      snum = (int) ((double) ((Math.random() * 1000))/100.0); 
      break; 
     } 

    } 
    public void requestFocus() { 
     // TODO Auto-generated method stub 

    } 
} 

私は、 "#" ボタンアプリケーションのクラッシュをクリックします。なぜどんなアイデア?ログなししかし、あなたはおそらくエラーになります

display.setText(fnum+"+"+ snum+"="); 

:コードからOK

+0

はfnumであり、snumは活性化されていますか?私はあなたがそれらを宣言するのを見ることができますが、私はあなたがそれらを置く場所を見ることができません。 –

+0

スタックトレースを送信します。 – kabuko

+0

@grahamにfnum = 0とsnum = 0に設定し、switch文でそれらを使用してfnum + snum = –

答えて

1

あなたは

int fnum, snum; 

を宣言したが、彼らはあなたが呼び出すポイントの前のコードのいずれかの場所に設定されていない与えられました/デバッグ情報私はあなたに伝えることはできません。これを確認するために投稿してください。

編集:display.setText後

...あなたはその後、値を設定します。それらを設定して表示する必要があります。

case R.id.keypad_hash: 

     fnum = (int) ((double) ((Math.random() * 1000))/100.0); 
     snum = (int) ((double) ((Math.random() * 1000))/100.0); 
     display.setText(fnum+"+"+ snum+"="); 
     break; 

次の編集:あなたはそれを行うことはできません

Random random = new Random(); 

fnum = random.nextInt(100); 
//nextInt(100) - upto the max value of 100 
snum = random.nextInt(100); 
+0

なぜ1000を掛けて100で割ったのですか?確かにあなたは10倍にしたいですか? –

+1

多くの算術演算子を使用していないと、コードが魅力的ではないからです。誰もが知っている。 – JakeWilson801

+1

Jon Skeetは、数学を見ればフィットします。「skeetから隠れる」ボタンはどこにありますか? –

2
View v = null; 
switch(v.getId()){ 

あなたはランダムなあなたはあなたのような何かを行うことができます作成​​されているint型についてあまりにもうるさいされていない場合。 vnullです。代わりにswitch(arg0.getId())を試してください。

EDIT:グラハム・スミスが指摘するようにまた、ラインを入れて...

display.setText(fnum+"+"+ snum+"="); 

...あなたは乱数を生成した後。

+0

ありがとうございました...それはスイッチ(arg0.getId())で完璧に働いていました.... –

関連する問題