2012-03-05 139 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 

    } 
} 

当我点击 “#” 号键,应用程序崩溃。任何想法为什么?

+0

是fnum和snum initalised?我可以看到你宣布他们,但我无法看到你设置他们的位置。 – 2012-03-05 20:22:37

+0

发布堆栈跟踪。 – kabuko 2012-03-05 20:23:36

+0

@graham,我将它们设置为fnum = 0和snum = 0,然后使用它们在switch语句中生成一个randon表达式,如fnum + snum = – 2012-03-05 20:30:14

回答

1

从代码OK给你申报

int fnum, snum; 

,但他们没有在代码设置,当你调用前点:

display.setText(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? – 2012-03-05 20:29:11

+1

因为如果你没有很多算术运算符,你的代码不会很酷。大家都知道。 – JakeWilson801 2012-03-05 20:33:10

+1

如果Jon Skeet看到数学问题 - “躲避双向飞行”按钮何去何从? – 2012-03-05 20:38:23

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

你不能做到这一点。 vnull。改为尝试switch(arg0.getId())

编辑:亦作格雷厄姆·史密斯指出,放线......

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

...当你已经产生的随机数。

+0

感谢小伙子......它使用switch(arg0.getId())完美工作.... – 2012-03-05 20:55:46