我的代码遇到一个奇怪的(对我来说)NullPointerException
错误,它仅在真实设备(两部手机和一台平板电脑)上发生,但是在emulators
上,它正常工作!
这是代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
logo = (ImageView) findViewById(R.id.logo);
flyIn()
}
private void flyIn() {
animation = AnimationUtils.loadAnimation(this, R.anim.logo_animation); //<-- this line here
logo.startAnimation(animation);
}
logo_animation
看起来像:<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true"
android:fillEnabled="true" >
<scale
android:duration="1500"
android:fromXScale="0.1"
android:fromYScale="0.1"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toXScale="1"
android:toYScale="1" />
<rotate
android:duration="1500"
android:fromDegrees="-270"
android:interpolator="@android:anim/accelerate_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="0" />
<alpha
android:duration="1500"
android:fromAlpha="0"
android:toAlpha="1" />
</set>
堆栈跟踪 :
06-21 23:46:07.844: E/AndroidRuntime(20983): FATAL EXCEPTION: main
06-21 23:46:07.844: E/AndroidRuntime(20983): Process: example.animation, PID: 20983
06-21 23:46:07.844: E/AndroidRuntime(20983): java.lang.RuntimeException: Unable to start activity ComponentInfo{example.animation/example.animation.Splash}: java.lang.NullPointerException
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2436)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2498)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread.access$900(ActivityThread.java:179)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.os.Handler.dispatchMessage(Handler.java:102)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.os.Looper.loop(Looper.java:146)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread.main(ActivityThread.java:5641)
06-21 23:46:07.844: E/AndroidRuntime(20983): at java.lang.reflect.Method.invokeNative(Native Method)
06-21 23:46:07.844: E/AndroidRuntime(20983): at java.lang.reflect.Method.invoke(Method.java:515)
06-21 23:46:07.844: E/AndroidRuntime(20983): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1288)
06-21 23:46:07.844: E/AndroidRuntime(20983): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1104)
06-21 23:46:07.844: E/AndroidRuntime(20983): at dalvik.system.NativeStart.main(Native Method)
06-21 23:46:07.844: E/AndroidRuntime(20983): Caused by: java.lang.NullPointerException
06-21 23:46:07.844: E/AndroidRuntime(20983): at example.animation.Splash.flyIn(Splash.java:67)
06-21 23:46:07.844: E/AndroidRuntime(20983): at example.animation.Splash.onCreate(Splash.java:47)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.Activity.performCreate(Activity.java:5484)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
06-21 23:46:07.844: E/AndroidRuntime(20983): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2400)
06-21 23:46:07.844: E/AndroidRuntime(20983): ... 11 more
(Splash.java:67)
这是这行:animation = AnimationUtils.loadAnimation(this, R.anim.logo_animation);
有人可以向我解释发生了什么吗?
最佳答案
@莎拉,
基于Android's Drawable Animation Guide,看来您无法将startAnimation()调用放入onCreate()中。该指南讨论了AnimationDrawable对象,但说
请务必注意,在
无法在以下对象的onCreate()方法期间调用AnimationDrawable
您的活动,因为AnimationDrawable尚未完全附加
到窗户。
我怀疑您的代码中正在发生类似的事情。如果您参考该指南,它将讨论不应该出现此问题的另一种启动动画的方法。
关于为什么在模拟器中工作;我很茫然。仿真器可能很奇怪。