Android:ImageView中的动画无法运行

时间:2016-02-03 19:12:01

标签: android android-imageview android-drawable

当我尝试去动画所在的活动时,它总是崩溃。我的日志猫在这里,我的其他文件都在这里。

我试图通过DrawableAnimation创建动画,但我不知道为什么在设置背景资源时出现错误

logcat的

02-03 18:52:30.323 4831-4831/com.secsys.gagacamaso.gagacamaso E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.secsys.gagacamaso.gagacamaso, PID: 4831
                                                                            java.lang.RuntimeException: Unable to start activity ComponentInfo{com.secsys.gagacamaso.gagacamaso/com.secsys.gagacamaso.gagacamaso.Menu}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                at android.os.Looper.loop(Looper.java:148)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                             Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference
                                                                                at com.secsys.gagacamaso.gagacamaso.Menu.onCreate(Menu.java:31)
                                                                                at android.app.Activity.performCreate(Activity.java:6237)
                                                                                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                                at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5417) 

Menu.java

public class Menu extends Activity {

ImageButton enable;
ImageView rm1;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.menu);
    setViews();
    enable = (ImageButton)findViewById(R.id.enable);
    lock();


    rm1.setBackgroundResource(R.drawable.rm_animation);

}

public void setViews(){
    ImageView rm1 = (ImageView)findViewById(R.id.rm1);
}

private void lock(){
    enable.setTag(1);
    enable.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final int state =(Integer) v.getTag();
            if(state == 1) {
                Toast.makeText(Menu.this, "ENABLED!", Toast.LENGTH_SHORT).show();
                enable.setBackgroundResource(R.drawable.disable);
                AnimationDrawable rmanim = (AnimationDrawable) rm1.getBackground();
                rmanim.start();
                v.setTag(0);
            } else {
                Toast.makeText(Menu.this, "DISABLED!", Toast.LENGTH_SHORT).show();
                enable.setBackgroundResource(R.drawable.enable);
                v.setTag(1); //pause
            }
        }
    });
}

rm_animation.xml(在drawable文件夹中)

<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:oneshot="true">
    <item android:drawable="@drawable/rm1" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow1" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow2" android:duration="150"/>
    <item android:drawable="@drawable/rm1glow3" android:duration="150"/>

</animation-list>

menu.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">


<GridLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="100dp"
    android:layout_centerHorizontal="true"
    android:id="@+id/gridLayout2">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm1"
        android:layout_row="0"
        android:layout_column="0"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm4"
        android:layout_row="0"
        android:layout_column="2"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm2"
        android:layout_row="1"
        android:layout_column="0"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm5"
        android:layout_row="1"
        android:layout_column="2"
        android:background="@drawable/rm1"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/rm3"
        android:layout_row="2"
        android:layout_column="0"
        android:background="@drawable/rm1"/>
    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/lrm1"
        android:layout_row="0"
        android:layout_column="1"
        android:layout_gravity="fill_vertical"
        android:layout_rowSpan="3"
        android:background="@drawable/rm2"/>

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:id="@+id/lrm2"
        android:layout_row="2"
        android:layout_column="2"
        android:background="@drawable/rm2a"/>

</GridLayout>

<ImageButton
    android:layout_width="200dp"
    android:layout_height="50dp"
    android:id="@+id/enable"
    android:layout_below="@+id/gridLayout2"
    android:layout_centerHorizontal="true"
    android:background="@drawable/enable"/>
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

boss这个空指针异常是因为

  public void setViews(){
    ImageView rm1 = (ImageView)findViewById(R.id.rm1);
  }

在这种情况下,用于设置背景的rm1的引用将始终为null。

尝试做

public void setViews(){
     rm1 = (ImageView)findViewById(R.id.rm1);
 }

答案 1 :(得分:0)

从setViews()方法中删除ImageView,因为它会创建一个局部变量并初始化它而不是顶级rm1

public void setViews(){
     rm1 = (ImageView)findViewById(R.id.rm1);
}

答案 2 :(得分:0)

您应首先从可绘制的xml文件创建动画,然后将其调用到ImageView。

Animation animation = AnimationUtils.loadAnimation(this, R.anim.rm_animation);
imageView.startAnimation(animation);

只需创建一个新的动画文件夹并创建新的动画文件,然后从您在drawable文件夹中的rm_animation.xml中复制所有内容。

setView() ImageView中删除。如果您已经在方法之外初始化变量,那么您应该只有imageView = (ImageView) findViewById(R.id.imageView);