运行启动画面时抛出的错误

时间:2016-04-18 22:29:12

标签: java android

我正在尝试为我的Android应用程序运行启动画面,我已经按照了几个教程,现在当我尝试运行代码时,会出现以下错误消息。如果有人能告诉我我缺少的代码或需要更改的内容,我们将非常感激。

我的Android监视器上显示的错误消息。

04-18 23:20:13.749 3395-3395/com.example.matthew.myapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                     Process: com.example.matthew.myapp, PID: 3395
                                                                     java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.matthew.myapp/com.example.matthew.myapp.SplashActivity}: 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:2416)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                         at android.os.Looper.loop(Looper.java:148)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                         at java.lang.reflect.Method.invoke(Native Method)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                      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.example.matthew.myapp.SplashActivity.onCreate(SplashActivity.java:21)
                                                                         at android.app.Activity.performCreate(Activity.java:6237)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                                                                         at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                         at android.os.Looper.loop(Looper.java:148) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5417) 
                                                                         at java.lang.reflect.Method.invoke(Native Method) 
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我的启动画面代码

package com.example.matthew.myapp;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.ImageView;


public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_splash);

    final ImageView iv = (ImageView) findViewById(R.id.imageView);
    final Animation an = AnimationUtils.loadAnimation(getBaseContext(),R.anim.rotate);
    final Animation an2 = AnimationUtils.loadAnimation(getBaseContext(),R.anim.abc_fade_out);

    iv.startAnimation(an);
    an.setAnimationListener(new Animation.AnimationListener() {
        @Override
        public void onAnimationStart(Animation animation) {

        }

        @Override
        public void onAnimationEnd(Animation animation) {
            iv.startAnimation(an2);
            finish();
            Intent i = new Intent(getBaseContext(),MainActivity.class);
            startActivity(i);
        }

        @Override
        public void onAnimationRepeat(Animation animation) {

        }
    });
}
}

0 个答案:

没有答案