Force close onClick of ImageButton, can't figure out why
I'm trying to do a simple animation with ImageButtons, when I set it up so that if I simply touch the screen, it works just fine, but if I try to do it onClick, the app just closes.
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>
The main Activity:
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); } }
edit:
I fixed the private void RunAnimations(View view)
But it's still force closing
edit 2: Contents of 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
The framework isn't finding your click handler method because it has the wrong signature. Declare it like this:
public void RunAnimations(View view) {
. . .
}
链接地址: http://www.djcxy.com/p/96148.html
上一篇: 使用导航抽屉与其他活动