简体   繁体   English

NullPointerException仅适用于某些设备上的动画

[英]NullPointerException for Animation on some devices only

Some devices throw a NullPointerException when launching this app, but others seem to work fine. 一些设备在启动此应用时会抛出NullPointerException,但其他设备似乎运行良好。 I cannot seem to figure out what to do as ivMonkeyStart that the error references is not null. 我似乎无法弄清楚该错误引用不为null ivMonkeyStart如何处理ivMonkeyStart I have tried different ways of setting ivMonkeyStart but have not found a way that makes a difference... 我尝试了设置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);
            }
        });
    }

Here is the logcat: 这是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); 该错误引用以下行: ivMonkeyStart.startAnimation(startMonkeyScaleIn);

Any idea? 任何想法? Thanks! 谢谢!

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? 也许您有两种布局,一种具有ivMonkeyStart ImageView,一种没有,而这些设备获得了第二种布局?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM