java - 动画android中的NullPointerException

标签 java android

我的代码出现了一个奇怪的(对我来说)NullPointerException 错误,它只发生在真实设备(两部手机和一部平板电脑)上,但在模拟器上,它工作得很好!!

代码如下:

@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 对象,但说

It's important to note that the start() method called on the AnimationDrawable cannot be called during the onCreate() method of your Activity, because the AnimationDrawable is not yet fully attached to the window.

我怀疑您的代码中发生了类似的事情。如果您引用该指南,它会讨论另一种启动动画的方法,这种方法不应该有这个问题。

至于为什么在模拟器中工作;我不知所措。模拟器可能很奇怪。

关于java - 动画android中的NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38000262/

相关文章:

android - Recycler view Array index out of bound Inconsistency Error while SwipeRefresh 和回收器同时滚动

Java Linux ServerSocket 没有做太多事情

java - 将变量放入 SQL 语句中 (Java/SQL)

javascript - 如何使用 html 和 javascript 开发原生 android 应用程序

android - 菜单项未显示在操作栏上

java - 从另一个类获取变量

java - Spring JMSListener - 它应该如何处理空负载?

java - 即使 "set"名称为空,Aerospike Java 客户端也会存储数据

java ArrayList 获取最大值

android - 打开软键盘时,Android Recyclerview Java.lang.NullPointerException