我开始关注Eclipse和Java的东西。我的应用程序有问题。我想做一个按钮来做两个不同的动作:旋转,缩放。不幸的是,当我按下该按钮时,应用已经停止。这是我的代码,请提出一些建议:
Main Activity
package com.example.ksztalty;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.*;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void stosuj_wszystko(View v)
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.wszystko_razem);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.startAnimation(a);
}
public void obracaj(View v)
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.obrot);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.startAnimation(a);
}
public void skaluj(View v)
{
Animation a = AnimationUtils.loadAnimation(this, R.anim.skalowanie);
ImageView iv = (ImageView)findViewById(R.id.imageView1);
iv.startAnimation(a);
}
}
和我的xml文件
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<rotate
android:fromDegrees="0"
android:toDegrees="720"
android:duration="1000"
android:pivotX="50%"
android:pivotY="50%"/>
<scale
android:fromXScale="1.0"
android:toXScale="2.0"
android:fromYScale="1.0"
android:toYScale="2.0"
android:pivotX="50%"
android:pivotY="50%"
android:duration="1000"/>
</set>
活性:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="150dp"
android:layout_marginTop="150dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/rybka1"
/>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="25dp"
android:layout_marginTop="31dp"
android:text="Obrót"
android:onClick="obracaj" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_toRightOf="@+id/button1"
android:text="Skala"
android:onClick="skaluj" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_toRightOf="@+id/button2"
android:onClick="zanikaj"
android:text="Zanik"/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button2"
android:layout_alignLeft="@+id/button1"
android:layout_alignRight="@+id/button3"
android:onClick="stosuj wszystko"
android:text="Wszystko razem" />
</RelativeLayout>
和错误日志:
11-08 10:43:47.605: E/AndroidRuntime(11645): FATAL EXCEPTION: main
11-08 10:43:47.605: E/AndroidRuntime(11645): Process: com.example.ksztalty, PID: 11645
11-08 10:43:47.605: E/AndroidRuntime(11645): java.lang.IllegalStateException: Could not find a method stosuj wszystko(View) in the activity class com.example.ksztalty.MainActivity for onClick handler on view class android.widget.Button with id 'button4'
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.view.View$1.onClick(View.java:4240)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.view.View.performClick(View.java:5197)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.view.View$PerformClick.run(View.java:20926)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.os.Handler.handleCallback(Handler.java:739)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.os.Handler.dispatchMessage(Handler.java:95)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.os.Looper.loop(Looper.java:145)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.app.ActivityThread.main(ActivityThread.java:5942)
11-08 10:43:47.605: E/AndroidRuntime(11645): at java.lang.reflect.Method.invoke(Native Method)
11-08 10:43:47.605: E/AndroidRuntime(11645): at java.lang.reflect.Method.invoke(Method.java:372)
11-08 10:43:47.605: E/AndroidRuntime(11645): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1388)
11-08 10:43:47.605: E/AndroidRuntime(11645): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1183)
11-08 10:43:47.605: E/AndroidRuntime(11645): Caused by: java.lang.NoSuchMethodException: stosuj wszystko [class android.view.View]
11-08 10:43:47.605: E/AndroidRuntime(11645): at java.lang.Class.getMethod(Class.java:665)
11-08 10:43:47.605: E/AndroidRuntime(11645): at android.view.View$1.onClick(View.java:4233)
11-08 10:43:47.605: E/AndroidRuntime(11645): ... 10 more
答案 0 :(得分:0)
您的方法名称是stosuj_wszystko
,并且您已经在.xml文件中的button4的onClick方法上设置了“stosuj wszystko”,只需输入正确的方法名称。