我正在尝试使用开关盒从我的按钮获取值。 但每当我使用这个代码时,它都会崩溃.. 有谁知道如何解决它。
此外,此代码尚未完成,因此我缺少7个按钮,但稍后会这样。
public void startGame(View v){
String randText = "";
String test = "10";
String test1 = "11";
Random randGen = new Random();
int rando = randGen.nextInt(10);
randText = Integer.toString(rando);
TextView RandomValue = (TextView) findViewById(R.id.randomValueTv);
RandomValue.setText(randText);
randomNumbersRbra();
TimerGame();
Button start = (Button)findViewById(R.id.startGameBtn);
start.setVisibility(View.INVISIBLE);
TextView go = (TextView)findViewById(R.id.tvGo);
go.setVisibility(View.VISIBLE);
Button btnTap1 = (Button)findViewById(R.id.tap1);
Button btnTap2 = (Button)findViewById(R.id.tap2);
Button btnTap3 = (Button)findViewById(R.id.tap3);
Button btnTap4 = (Button)findViewById(R.id.tap4);
Button btnTap5 = (Button)findViewById(R.id.tap5);
Button btnTap6 = (Button)findViewById(R.id.tap6);
Button btnTap7 = (Button)findViewById(R.id.tap7);
Button btnTap8 = (Button)findViewById(R.id.tap8);
Button btnTap9 = (Button)findViewById(R.id.tap9);
TextView tvtest = (TextView)findViewById(R.id.scoreTv);
tvtest.setText(test.toString());
switch(v.getId())
{
case R.id.tap1:
tvtest.setText(test1.toString());
break;
case R.id.tap2:
tvtest.setText(test1.toString());
}
btnTap1.setOnClickListener((OnClickListener) this);
btnTap2.setOnClickListener((OnClickListener) this);
}
logcat的
> 05-22 14:06:22.271: D/dalvikvm(29893): Late-enabling CheckJNI
05-22 14:06:22.821: D/libEGL(29893): loaded /system/lib/egl/libEGL_mali.so
05-22 14:06:22.821: D/libEGL(29893): loaded /system/lib/egl/libGLESv1_CM_mali.so
05-22 14:06:22.881: D/libEGL(29893): loaded /system/lib/egl/libGLESv2_mali.so
05-22 14:06:22.941: D/OpenGLRenderer(29893): Enabling debug mode 0
05-22 14:06:24.753: D/dalvikvm(29893): JIT code cache reset in 13 ms (4096 bytes 2/0)
05-22 14:06:24.763: D/dalvikvm(29893): GC_CONCURRENT freed 172K, 7% free 11024K/11783K, paused 3ms+26ms, total 127ms
05-22 14:06:25.283: E/SpannableStringBuilder(29893): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 14:06:25.283: E/SpannableStringBuilder(29893): SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
05-22 14:06:26.034: D/AndroidRuntime(29893): Shutting down VM
05-22 14:06:26.044: W/dalvikvm(29893): threadid=1: thread exiting with uncaught exception (group=0x416c92a0)
05-22 14:06:26.064: E/AndroidRuntime(29893): FATAL EXCEPTION: main
05-22 14:06:26.064: E/AndroidRuntime(29893): java.lang.IllegalStateException: Could not execute method of the activity
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.view.View$1.onClick(View.java:3660)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.view.View.performClick(View.java:4162)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.view.View$PerformClick.run(View.java:17082)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.os.Handler.handleCallback(Handler.java:615)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.os.Handler.dispatchMessage(Handler.java:92)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.os.Looper.loop(Looper.java:137)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.app.ActivityThread.main(ActivityThread.java:4867)
05-22 14:06:26.064: E/AndroidRuntime(29893): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 14:06:26.064: E/AndroidRuntime(29893): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 14:06:26.064: E/AndroidRuntime(29893): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
05-22 14:06:26.064: E/AndroidRuntime(29893): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
05-22 14:06:26.064: E/AndroidRuntime(29893): at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
05-22 14:06:26.064: E/AndroidRuntime(29893): at dalvik.system.NativeStart.main(Native Method)
05-22 14:06:26.064: E/AndroidRuntime(29893): Caused by: java.lang.reflect.InvocationTargetException
05-22 14:06:26.064: E/AndroidRuntime(29893): at java.lang.reflect.Method.invokeNative(Native Method)
05-22 14:06:26.064: E/AndroidRuntime(29893): at java.lang.reflect.Method.invoke(Method.java:511)
05-22 14:06:26.064: E/AndroidRuntime(29893): at android.view.View$1.onClick(View.java:3655)
05-22 14:06:26.064: E/AndroidRuntime(29893): ... 12 more
05-22 14:06:26.064: E/AndroidRuntime(29893): Caused by: java.lang.ClassCastException: com.roc.quickynumbers.Game2 cannot be cast to android.view.View$OnClickListener
05-22 14:06:26.064: E/AndroidRuntime(29893): at com.roc.quickynumbers.Game2.startGame(Game2.java:121)
05-22 14:06:26.064: E/AndroidRuntime(29893): ... 15 more
提前致谢!
答案 0 :(得分:0)
首先确保在您的班级中实施onclicklistener ....
public void startGame(View v){
String randText = "";
String test = "10";
String test1 = "11";
Random randGen = new Random();
int rando = randGen.nextInt(10);
randText = Integer.toString(rando);
TextView RandomValue = (TextView) findViewById(R.id.randomValueTv);
RandomValue.setText(randText);
randomNumbersRbra();
TimerGame();
Button start = (Button)findViewById(R.id.startGameBtn);
start.setVisibility(View.INVISIBLE);
TextView go = (TextView)findViewById(R.id.tvGo);
go.setVisibility(View.VISIBLE);
Button btnTap1 = (Button)findViewById(R.id.tap1);
Button btnTap2 = (Button)findViewById(R.id.tap2);
Button btnTap3 = (Button)findViewById(R.id.tap3);
Button btnTap4 = (Button)findViewById(R.id.tap4);
Button btnTap5 = (Button)findViewById(R.id.tap5);
Button btnTap6 = (Button)findViewById(R.id.tap6);
Button btnTap7 = (Button)findViewById(R.id.tap7);
Button btnTap8 = (Button)findViewById(R.id.tap8);
Button btnTap9 = (Button)findViewById(R.id.tap9);
TextView tvtest = (TextView)findViewById(R.id.scoreTv);
tvtest.setText(test.toString());
//OnClickListener like below:
btnTap1.setOnClickListener(this);
btnTap2.setOnClickListener(this);
}
//And override method like below:
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tap1:
// do something
break;
case R.id.tap2:
//do something
break;
default:
break;
}
}
答案 1 :(得分:0)
Activity实现OnClickListener。
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.tap1:
tvtest.setText(test1.toString());
break;
case R.id.tap2:
tvtest.setText(test1.toString());
break;
default:
break;
}
}