我正在尝试创建需要登录的应用程序。为了测试它的工作原理,我制作了一个登录按钮以转到单独的布局,在该布局中,您可以按按钮对数字进行随机化。登录按钮可以正常工作,将我带到另一个布局,但是当我单击单独布局上的按钮时,我的应用程序崩溃了。
这是我的代码:
private TextView R1;
private TextView R2;
private TextView R3;
private TextView R4;
private TextView R5;
private TextView R6;
private Animation R1_roll_rotate;
private Animation R2_roll_rotate;
private Animation R3_roll_rotate;
private Animation R4_roll_rotate;
private Animation R5_roll_rotate;
private Animation R6_roll_rotate;
private Button Randomize_Button;
private Animation Button_Rotate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Randomize_Button= (Button) findViewById(R.id.Randomize);
R1= (TextView) findViewById(R.id.RandomOne);
R2= (TextView) findViewById(R.id.RandomTwo);
R3= (TextView) findViewById(R.id.RandomThree);
R4= (TextView) findViewById(R.id.RandomFour);
R5= (TextView) findViewById(R.id.RandomFive);
R6= (TextView) findViewById(R.id.RandomSix);
Button_Rotate= AnimationUtils.loadAnimation(Main_Activity.this, R.anim.Button_Rotate);
R1_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomOne_roll_rotate);
R2_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomTwo_roll_rotate);
R3_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomThree_roll_rotate);
R4_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFour_roll_rotate);
R5_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomFive_roll_rotate);
R6_roll_rotate = AnimationUtils.loadAnimation(Main_Activity.this, R.anim.RandomSix_roll_rotate);
public void onClick(View view) {
int randomnum1 = r.nextInt(70);
int randomnum2 = r.nextInt(70);
int randomnum3 = r.nextInt(70);
int randomnum4 = r.nextInt(70);
int randomnum5 = r.nextInt(70);
int randomnum6 = r.nextInt(27);
int num1 = randomnum1;
int num2 = randomnum2;
int num3 = randomnum3;
int num4 = randomnum4;
int num5 = randomnum5;
int num6 = randomnum6;
String randomnum7 = String.valueOf(randomnum1);
String randomnum8 = String.valueOf(randomnum2);
String randomnum9 = String.valueOf(randomnum3);
String randomnum10 = String.valueOf(randomnum4);
String randomnum11 = String.valueOf(randomnum5);
String randomnum12 = String.valueOf(randomnum6);
try {
Randomize_Button.startAnimation(Button_Rotate);
R1.startAnimation(R1_roll_rotate);
R2.startAnimation(R2_roll_rotate);
R3.startAnimation(R3_roll_rotate);
R4.startAnimation(R4_roll_rotate);
R5.startAnimation(R5_roll_rotate);
R6.startAnimation(R6_roll_rotate);*/
for (int i = num1; i > 0 && i == num1; i++) {
String ii = Integer.toString(i);
R1.setText(ii);
}
for (int j = num2; j > 0 && j == num2; j++) {
String jj = Integer.toString(j);
R2.setText(jj);
}
for (int k = num3; k > 0 && k == num3; k++) {
String kk = Integer.toString(k);
R3.setText(kk);
}
for (int d = num4; d > 0 && d == num4; d++) {
String dd = Integer.toString(d);
R4.setText(dd);
}
for (int s = num5; s > 0 && s == num5; s++) {
String ss = Integer.toString(s);
R5.setText(ss);
}
for (int g = num6; g > 0 && g == num6; g++) {
String gg = Integer.toString(g);
R6.setText(gg);
}
for (int g = num1; g == num2; g++) {
g = r.nextInt(70);
String gg = Integer.toString(g);
R1.setText(gg);
}
for (int d = num1; d == num3; d++) {
d = r.nextInt(70);
String dd = Integer.toString(d);
R1.setText(dd);
}
for (int a = num1; a == num3; a++) {
a = r.nextInt(70);
String aa = Integer.toString(a);
R1.setText(aa);
}
for (int w = num1; w == num4; w++) {
w = r.nextInt(70);
String ww = Integer.toString(w);
R1.setText(ww);
}
for (int q = num1; q == num5; q++) {
q = r.nextInt(70);
String qq = Integer.toString(q);
R1.setText(qq);
}
for (int y = num2; y == num3; y++) {
y = r.nextInt(70);
String yy = Integer.toString(y);
R2.setText(yy);
}
for (int o = num2; o == num4; o++) {
o = r.nextInt(70);
String oo = Integer.toString(o);
R2.setText(oo);
}
for (int p = num2; p == num5; p++) {
p = r.nextInt(70);
String pp = Integer.toString(p);
R2.setText(pp);
}
for (int x = num3; x == num4; x++) {
x = r.nextInt(70);
String xx = Integer.toString(x);
R3.setText(xx);
}
for (int c = num3; c == num5; c++) {
c = r.nextInt(70);
String cc = Integer.toString(c);
R3.setText(cc);
}
for (int b = num4; b == num5; b++) {
b = r.nextInt(70);
String bb = Integer.toString(b);
R4.setText(bb);
}
} catch (Exception e){
Toast.makeText(Main_Activity.this,e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
有人知道为什么吗?如果是这样,将非常感谢您的帮助!谢谢。
答案 0 :(得分:0)
我弄清楚阅读您的问题的可能原因很可能是您忘记了在其他布局中添加按钮。
因为在这种情况下,按钮视图将为空,因此一旦单击,它将抛出NullPointerException
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setEnabled(boolean)' on a null object reference
这可能是您遇到崩溃的原因。
希望这会有所帮助。
答案 1 :(得分:0)
在按钮点击之类的按钮中添加一个try-catch块。
try{
//Your Code for randomize number.
}
catch(Exception e)
{
Toast.makeText(context,e.getMessage(), Toast.LENGTH_SHORT).show();
}
这将在吐司中显示异常,并且您的应用程序不会崩溃。