zngb
zngb

Reputation: 633

NullPointerException for Animation on some devices only

Some devices throw a NullPointerException when launching this app, but others seem to work fine. I cannot seem to figure out what to do as ivMonkeyStart that the error references is not null. I have tried different ways of setting ivMonkeyStart but have not found a way that makes a difference...

public class MainActivity extends Activity {
    ImageView ivMonkeyStart;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);
        ivMonkeyStart = (ImageView)findViewById(R.id.ivMonkeyStart);
        final Animation startMonkeyScaleIn = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.start_scale);
        final Animation startMonkeyPulse = AnimationUtils.loadAnimation(this, R.anim.start_pulse);

        ivMonkeyStart.startAnimation(startMonkeyScaleIn);

        startMonkeyScaleIn.setAnimationListener(new Animation.AnimationListener() {
            @Override
            public void onAnimationStart(Animation animation) {

            }

            @Override
            public void onAnimationEnd(Animation animation) {
                ivMonkeyStart.startAnimation(startMonkeyPulse);
            }

            @Override
            public void onAnimationRepeat(Animation animation) {

            }
        });

        ivMonkeyStart.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                FlurryAgent.logEvent("Clicked Start");
                Intent myIntent = new Intent(MainActivity.this, GuessActivity.class);
                MainActivity.this.startActivity(myIntent);
            }
        });
    }

Here is the logcat:

01-01 22:59:56.658    2188-2188/com.zdkapps.guesszoo E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.zdkapps.guesszoo, PID: 2188
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.zdkapps.guesszoo/com.zdkapps.guesszoo.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.startAnimation(android.view.animation.Animation)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.startAnimation(android.view.animation.Animation)' on a null object reference
            at com.zdkapps.guesszoo.MainActivity.onCreate(MainActivity.java:49)
            at android.app.Activity.performCreate(Activity.java:5933)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

The error references this line: ivMonkeyStart.startAnimation(startMonkeyScaleIn);

Any idea? Thanks!

Upvotes: 0

Views: 1417

Answers (1)

Zielony
Zielony

Reputation: 16537

Copied from a comment for the question/answer mechanism:

What are these devices? Is there any specific difference? Maybe you have two layouts, one with the ivMonkeyStart ImageView and one without, and these devices get the second layout?

Upvotes: 2

Related Questions