我在屏幕上显示了一个表达“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”时应用程序崩溃
真的很感激帮助
答案 0 :(得分:2)
我不知道你是否发布了整个代码,但从我看到你永远不会初始化变量“display”(TextView显示;)。然后当你使用它时,你会得到一个NullPointerException。
我想你错过了像display =(TextView)findViewById(R.id.display ..);
这样的行