2013-10-25 47 views
1

出於某種原因,我無法找出爲什麼在點擊我的ToggleButton後我的項目沒有響應。已經嘗試了一切,似乎無法實現它的功能。有任何想法嗎?當我點擊ToggleButton並且調用函數時,Android項目崩潰

佈局我的切換按鈕:

<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 
+0

我有我的剪貼板上的logcat信息。我在哪裏發佈?這是新的。對不起 –

+0

發佈您的課程和Logcat。 –

回答

3

Android Developers

您在android:onClick屬性中聲明的方法必須具有完全如上所示的簽名。具體而言,該方法必須:

  • 是公共
  • 返回void
  • 定義視圖作爲其唯一的參數(這將是爲點擊查看)

嘗試

public void ToggleSound(View v){ 
    // ... 
} 
0

我用了一個主要活動的應用程序加載不同nt碎片活動與他們的佈局。 所以這種方法應該在主要活動

相關問題