仅限某些设备上的动画的NullPointerException

时间:2015-01-01 23:09:34

标签: java android nullpointerexception

某些设备在启动此应用时会抛出NullPointerException,但其他设备似乎工作正常。我似乎无法弄清楚错误引用的ivMonkeyStart做什么不是null。我尝试了不同的设置ivMonkeyStart的方法,但没有找到一种有所作为的方法......

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);
            }
        });
    }

这是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)

错误引用此行: ivMonkeyStart.startAnimation(startMonkeyScaleIn);

有什么想法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

从问题/答案机制的评论中复制:

这些设备是什么?有什么特别的区别吗?也许你有两个布局,一个是ivMonkeyStart ImageView,一个没有,这些设备得到第二个布局?