【问题标题】:Using a switch case to write button values to variable使用开关盒将按钮值写入变量
【发布时间】:2014-05-22 12:18:24
【问题描述】:

我正在尝试使用开关盒从我的按钮中获取值。 但是每次我使用这段代码时它都会崩溃.. 有谁知道如何解决它。

另外,这段代码还没有完成,所以我错过了 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

提前致谢!

【问题讨论】:

  • 你的Activity(或任何方法)没有实现OnClickListener
  • 更改:btnTap1.setOnClickListener(this);
  • Activity 实现 OnClickListener
  • 然后我收到此错误:View 类型中的方法 setOnClickListener(View.OnClickListener) 不适用于参数
  • 在实现OnclickListener之后,是Shows添加未实现的方法。这样做

标签: java android switch-statement case


【解决方案1】:

首先确保在你的类中实现 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;
    }
}

【讨论】:

  • 哇。这就是我需要的,我现在可以使用了!非常感谢
  • 是的,我会这样做!哈哈
【解决方案2】:

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;
    }

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-17
    • 1970-01-01
    • 2020-09-02
    • 1970-01-01
    相关资源
    最近更新 更多