1

出于某种原因,我无法弄清楚为什么我的项目在单击我的ToggleButton. 已经尝试了一切,似乎无法让它发挥作用。有任何想法吗?

我在布局中的切换按钮:

<ToggleButton
    android:id="@+id/tglSound"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:onClick="ToggleSound"
    android:textOff="Sound Off"
    android:textOn="Sound On"
    android:textSize="20sp"
    android:textStyle="bold" />

单击切换按钮并调用此函数时项目崩溃:

public void ToggleSound(){
    if (Global.btnSound.isChecked()){
        Global.snd.start();
    }
    else{
        Global.snd.pause();
    }
}

堆栈跟踪:

10-25 01:29:22.760: E/AndroidRuntime(18025): FATAL EXCEPTION: main
10-25 01:29:22.760: E/AndroidRuntime(18025): java.lang.IllegalStateException: Could not find a method ToggleSound(View) in the activity class com.garrenkeith.project.Menu for onClick handler on view class android.widget.ToggleButton with id 'tglSound'
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.view.View$1.onClick(View.java:3686)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.view.View.performClick(View.java:4223)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.widget.CompoundButton.performClick(CompoundButton.java:105)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.view.View$PerformClick.run(View.java:17281)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.os.Handler.handleCallback(Handler.java:615)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.os.Looper.loop(Looper.java:137)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.app.ActivityThread.main(ActivityThread.java:4898)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at java.lang.reflect.Method.invokeNative(Native Method)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at java.lang.reflect.Method.invoke(Method.java:511)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at dalvik.system.NativeStart.main(Native Method)
10-25 01:29:22.760: E/AndroidRuntime(18025): Caused by: java.lang.NoSuchMethodException: ToggleSound [class android.view.View]
10-25 01:29:22.760: E/AndroidRuntime(18025):    at java.lang.Class.getConstructorOrMethod(Class.java:460)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at java.lang.Class.getMethod(Class.java:915)
10-25 01:29:22.760: E/AndroidRuntime(18025):    at android.view.View$1.onClick(View.java:3679)
10-25 01:29:22.760: E/AndroidRuntime(18025):    ... 12 more
4

2 回答 2

3

来自Android 开发者

您在 android:onClick 属性中声明的方法必须具有完全如上所示的签名。具体来说,该方法必须:

  • 公开
  • 返回无效
  • 定义一个视图作为它的唯一参数(这将是被点击的视图)

尝试

public void ToggleSound(View v){
    // ...
}
于 2013-10-25T06:34:50.700 回答
0

我使用了一个带有 on main 活动的应用程序,它使用它们的布局加载不同的 Fragments 活动。所以这个方法应该在主要活动中

于 2014-01-09T09:30:14.693 回答