随机数1-6

时间:2011-11-23 20:03:56

标签: java android

我试图创建一个简单的高/低应用程序,但当我尝试获取随机数时应用程序崩溃。 这是一些代码:

final TextView counter = (TextView) findViewById(R.id.tcounter);
Button high = (Button) findViewById(R.id.bhigh);
Button low = (Button) findViewById(R.id.blow);
Button start = (Button) findViewById(R.id.bplay);
final Random rstart = new Random();
final int counternr = rstart.nextInt(5) + 1;

start.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            counter.setText(counternr);

应该将TextView“counter”设置为1-6中的随机数,但会让我崩溃。

提前致谢!

编辑:这是错误文本:

11-23 20:17:40.509: ERROR/AndroidRuntime(765): FATAL EXCEPTION: main
11-23 20:17:40.509: ERROR/AndroidRuntime(765): java.lang.RuntimeException: Unable to start activity ComponentInfo{xxx.xxx.xxx/xxx.xxx.xxx.play}: java.lang.NullPointerException
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.os.Looper.loop(Looper.java:123)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread.main(ActivityThread.java:3683)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at java.lang.reflect.Method.invokeNative(Native Method)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at java.lang.reflect.Method.invoke(Method.java:507)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at dalvik.system.NativeStart.main(Native Method)
11-23 20:17:40.509: ERROR/AndroidRuntime(765): Caused by: java.lang.NullPointerException
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at xxx.xxx.xxx.play.onCreate(play.java:26)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
11-23 20:17:40.509: ERROR/AndroidRuntime(765):     ... 11 more

非常凌乱,但我希望没关系。

2 个答案:

答案 0 :(得分:7)

更改此

counter.setText(counternr);

用这个

counter.setText(Integer.toString(counternr));

方法setText仅接受CharSequence个对象作为参数(在本例中为String),如果它代表资源ID,则只接受int

答案 1 :(得分:1)

确保在setContentView(...)findViewById(...)使用Buttons之前致电Textview

如果你不这样做,他们就不会被发现,他们都将无效。在这种情况下,尝试设置start.OnClickListener(...)会抛出NullPointerException