Я хочу отобразить случайное сгенерированное выражение, подобное 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
}
}
При нажатии на кнопку "#" сбои приложений. Любые идеи почему?
fnum и snum initalised? Я вижу, что вы объявляете их, но я не вижу, где вы их устанавливаете. –
Опубликовать трассировку стека. – kabuko
@graham, я устанавливаю их в fnum = 0 и snum = 0 и im, используя их в выражении switch для генерации выражения randon, например fnum + snum = –