由NullPointerException引起的android动画崩溃

时间:2014-04-14 12:26:01

标签: android animation crash

我一直在尝试运行动画,但它因某些我无法理解的原因而崩溃。 在log cat中它写因为nullpointerexception。但我不明白 我该怎么做\改变它。 我怎样才能解决这个问题?感谢名单!

MainActivity:

package nir.rauch.again;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Animation anim = AnimationUtils.loadAnimation(getBaseContext(),
                R.anim.correctanim);
        ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);
        duck.startAnimation(anim);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="155dp"
        android:contentDescription="Duck Animation"
        android:src="@drawable/duckwithswaves" />

</RelativeLayout>

anim xml:

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator" >


    <rotate
        android:duration="1000"
        android:fromDegrees="0"
        android:interpolator="@android:anim/cycle_interpolator"
        android:pivotX="50%"
        android:pivotY="50%"
        android:startOffset="3800"
        android:repeatCount="infinite"
        android:repeatMode="restart"
        android:toDegrees="360" />

</set>

logcat的:

04-14 15:19:23.068: E/AndroidRuntime(21445): FATAL EXCEPTION: main
04-14 15:19:23.068: E/AndroidRuntime(21445): java.lang.RuntimeException: Unable to start activity ComponentInfo{nir.rauch.again/nir.rauch.again.MainActivity}: java.lang.NullPointerException
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread.access$700(ActivityThread.java:159)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.os.Looper.loop(Looper.java:137)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at java.lang.reflect.Method.invokeNative(Native Method)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at java.lang.reflect.Method.invoke(Method.java:525)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at dalvik.system.NativeStart.main(Native Method)
04-14 15:19:23.068: E/AndroidRuntime(21445): Caused by: java.lang.NullPointerException
04-14 15:19:23.068: E/AndroidRuntime(21445):    at nir.rauch.again.MainActivity.onCreate(MainActivity.java:19)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.Activity.performCreate(Activity.java:5372)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
04-14 15:19:23.068: E/AndroidRuntime(21445):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
04-14 15:19:23.068: E/AndroidRuntime(21445):    ... 11 more

3 个答案:

答案 0 :(得分:3)

试试这个..

findViewById应包含ImageView

的ID

更改此

ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);

ImageView duck = (ImageView) findViewById(R.id.idofimageview);

答案 1 :(得分:1)

它位于您的图片视图中,您使用ImageView引用了R.drawable,而它应该是R.id

更改行

 ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);

如下:

ImageView duck = (ImageView) findViewById(R.id.yourimageid);

答案 2 :(得分:0)

更改行

 ImageView duck = (ImageView) findViewById(R.drawable.duckwithswaves);

 ImageView duck = (ImageView) findViewById(R.id.imageView1);