2012-03-07 20 views
0

「2 + 3 =?」という画面に式が表示されました。そして私は何をしたい、私は5を押したときに疑問符が5に置き換えますと、私は ボタンの押下時に文字を数字に置き換える方法

緑色で「正しい」
package org.example.question; 

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. */ 

    //variable for different questions 

    //variable and type declaration for buttons and text 

TextView display; 
TextView displayGuess; 
TextView displayQuestion; 
TextView Correct; 
TextView guess; 
TextView question; 


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

     //display text on screen 
     displayGuess = (TextView) findViewById(R.id.Guess); 
     displayQuestion = (TextView) findViewById(R.id.Question); 


     //Creating and setting onClickerLIstener on each button 
     Button one = (Button) findViewById(R.id.keypad_1); 
     one.setOnClickListener(this); 

     Button two = (Button) findViewById(R.id.keypad_2); 
     two.setOnClickListener(this); 

     Button three = (Button) findViewById(R.id.keypad_3); 
     three.setOnClickListener(this); 

     Button four = (Button) findViewById(R.id.keypad_4); 
     four.setOnClickListener(this); 

     Button five = (Button) findViewById(R.id.keypad_5); 
     five.setOnClickListener(this); 

     Button six = (Button) findViewById(R.id.keypad_6); 
     six.setOnClickListener(this); 

     Button seven = (Button) findViewById(R.id.keypad_7); 
     seven.setOnClickListener(this); 

     Button eight = (Button) findViewById(R.id.keypad_8); 
     eight.setOnClickListener(this); 

     Button nine = (Button) findViewById(R.id.keypad_9); 
     nine.setOnClickListener(this); 

     Button del = (Button) findViewById(R.id.keypad_delete); 
     del.setOnClickListener(this); 

     Button zero = (Button) findViewById(R.id.keypad_0); 
     zero.setOnClickListener(this); 

     Button hash = (Button) findViewById(R.id.keypad_hash); 
     hash.setOnClickListener(this); 

     Button minus = (Button) findViewById(R.id.keypad_minus); 
     minus.setOnClickListener(this); 

    } 

     //method to tell what each button does 
     public void onClick(View argh0) { 



      switch(argh0.getId()) { 

      case R.id.keypad_hash: 

       if("2+3=5".equals(display.getText().toString())) 
       { 
        display.setText("CORRECT".setColor."#00ff00"); 

       } 
       break; 

      case R.id.keypad_5: 
       String str5 = display.getText().toString(); 
       display.setText(str5.replace("?","5")); 
       break; 






      case R.id.keypad_1: 

       display.setText("1"); 
      break; 
      } 
     } 
} 

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
    <TextView 
     android:id="@+id/Guess" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="Guess: " 
     android:textSize="25dp" /> 
    <TextView 
     android:id="@+id/Question" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="2+3=?" 
     android:textSize="25dp" /> 
    <TextView 
     android:id="@+id/CORRECT" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="" 
     android:textSize="25dp" /> 
    <TextView 
     android:id="@+id/INCORRECT" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:layout_marginLeft="15dp" 
     android:text="" 
     android:textSize="25dp" /> 









<TableLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/keypad" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:stretchColumns="*" > 




<TableRow> 
<Button android:id="@+id/keypad_1" 
android:text="1" > 
</Button> 
<Button android:id="@+id/keypad_2" 
android:text="2" > 
</Button> 
<Button android:id="@+id/keypad_3" 
android:text="3" > 
</Button> 
</TableRow> 
<TableRow> 
<Button android:id="@+id/keypad_4" 
android:text="4" > 
</Button> 
<Button android:id="@+id/keypad_5" 
android:text="5" > 
</Button> 

を#ディスプレイを押したときに、これまで私は、質問を表示することができますが、私は「5」を押したときですアプリケーションのクラッシュ

は本当に私はあなたが全体のコードを掲示しているか否かを知っているが、私はあなたが変数「表示」(のTextView表示を;)初期化することはありません表示されるものとしていない助け

+0

あなたのlogcatを表示 –

+0

あなたのonClickメソッドの前に '@ Override'を設定しましたか? – jmishra

+0

私のonClickメソッドの前に@overrideを追加するとエラーが発生し、それを削除するとエラーが発生します。 –

答えて

2

に感謝します。それを使用すると、NullPointerExceptionが発生します。

私はあなたがdisplay =(TextView)findViewById(R.id.display ..)のような行を見逃したと思います。

+0

これは私のコード全体ですが、私はその変更を加えましたが、アプリケーションはクラッシュしませんが、 "?"を置き換えて "5"を印刷していない、 "R.id.keypad_5"正しいも.. –

関連する問題