2012-03-08 77 views
2

我試圖做一個簡單的動畫與ImageButtons,當我設置它,以便如果我只是觸摸屏幕,它的工作原理就好,但如果我嘗試要做到onClick,應用程序就會關閉。強制關閉onClick的ImageButton,無法弄清楚爲什麼

main.xml中

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_horizontal"> 
     <ImageButton 
     android:id="@+id/plus1" 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:background="@null" 
     android:src="@drawable/plus" 
     android:visibility="visible" 
     android:gravity="center_horizontal" 
     android:onClick="RunAnimations" 
     /> 

    <ImageButton 
     android:id="@+id/gears1" 
     android:layout_width="75dp" 
     android:layout_height="75dp" 
     android:layout_alignParentBottom="true" 
     android:layout_alignParentRight="true" 
     android:background="@null" 
     android:gravity="center_horizontal" 
     android:src="@drawable/gears" 
     android:textSize="42sp" /> 
    </LinearLayout> 

translate.xml

<?xml version="1.0" encoding="utf-8"?> 
<set> 
<translate 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromYDelta="-100%" 
    android:toYDelta="0%" 
    android:duration="1000" 
    android:zAdjustment="top" > 
</translate> 

<alpha 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:fromAlpha="0.0" 
    android:toAlpha="1.0" 
    android:duration="1000"> 
</alpha> 
</set> 

主要活動:

 

    public class AnimationtestActivity extends Activity { 
     /** Called when the activity is first created. */ 
     @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.main); 
     } 
     private void RunAnimations() { 
      Animation a = AnimationUtils.loadAnimation(this, R.anim.translate); 
      View ib = findViewById(R.id.gears1); 
      ib.clearAnimation(); 
      ib.startAnimation(a);  
     } 
    } 

編輯:

我固定 私人無效茹nAnimations(查看視圖)

但它仍然是強制關閉

編輯2:logcat的

03-08 13:07:33.968: E/AndroidRuntime(29270): FATAL EXCEPTION: main 
03-08 13:07:33.968: E/AndroidRuntime(29270): java.lang.IllegalStateException: Could not find a method RunAnimations(View) in the activity class com.test.AnimationtestActivity for onClick handler on view class android.widget.ImageButton with id 'plus1' 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.view.View$1.onClick(View.java:3026) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.view.View.performClick(View.java:3480) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.view.View$PerformClick.run(View.java:13983) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.os.Handler.handleCallback(Handler.java:605) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.os.Handler.dispatchMessage(Handler.java:92) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.os.Looper.loop(Looper.java:137) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.app.ActivityThread.main(ActivityThread.java:4340) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at java.lang.reflect.Method.invokeNative(Native Method) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at java.lang.reflect.Method.invoke(Method.java:511) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at dalvik.system.NativeStart.main(Native Method) 
03-08 13:07:33.968: E/AndroidRuntime(29270): Caused by: java.lang.NoSuchMethodException: RunAnimations [class android.view.View] 
03-08 13:07:33.968: E/AndroidRuntime(29270): at java.lang.Class.getConstructorOrMethod(Class.java:460) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at java.lang.Class.getMethod(Class.java:915) 
03-08 13:07:33.968: E/AndroidRuntime(29270): at android.view.View$1.onClick(View.java:3019) 
03-08 13:07:33.968: E/AndroidRuntime(29270): ... 11 more 

回答

3

框架沒有找到您單擊處理方法,因爲它的簽名錯誤的內容。聲明它是這樣的:

public void RunAnimations(View view) { 
    . . . 
} 
+0

我修正了這個問題,但它仍然給我一個力量關閉。任何其他想法? – Lucas 2012-03-08 18:07:58

+0

你收到的例外情況是什麼?你可以發佈日誌嗎? – arjoan 2012-03-08 18:16:11

+0

我在問題中發佈了logcat,感謝迄今的幫助 – Lucas 2012-03-08 18:22:39

相關問題