开始一个带有启动画面的应用程序,但当我运行它时。

huangapple go评论73阅读模式
英文:

starting an app with splash screen but when I run it

问题

我遇到了这个错误**"java.lang.NullPointerException: 尝试在空对象引用上调用虚拟方法'void android.widget.ImageView.setAnimation(android.view.animation.Animation)'****

public class MainActivity extends AppCompatActivity {
       
    private static int SPLASH_SCREEN = 5000;
    Animation topAnim, bottomAnim;
    ImageView image;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.activity_main);
        topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
        bottomAnim = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);

        ImageView Logo = findViewById(R.id.Logo);
        TextView slogan = findViewById(R.id.slogan);

        image.setAnimation(topAnim);
        slogan.setAnimation(bottomAnim);
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                Intent intent = new Intent(MainActivity.this, Dashboard.class);
                startActivity(intent);
                finish();
            }
        }, SPLASH_SCREEN);
    }
}
英文:

I get this error**"java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setAnimation(android.view.animation.Animation)' on a null object reference"****

public class MainActivity extends AppCompatActivity {
   
 private static int SPLASH_SCREEN = 5000;
    Animation topAnim,bottomAnim;
    ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);
    topAnim = AnimationUtils.loadAnimation(this, R.anim.top_animation);
    bottomAnim = AnimationUtils.loadAnimation(this, R.anim.bottom_animation);


    ImageView Logo = findViewById(R.id.Logo);
    TextView slogan = findViewById(R.id.slogan);

    image.setAnimation(topAnim);
    slogan.setAnimation(bottomAnim);
    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            Intent intent = new Intent(MainActivity.this,Dashboard.class);
            startActivity(intent);
            finish();
        }
    },SPLASH_SCREEN);
}

}

答案1

得分: 2

变量 image 没有初始化!

英文:

Variable image didn't initialize!

答案2

得分: 1

你没有将图像与你的xml匹配

image = findViewById(R.id.Logo);

而应该是

ImageView Logo = findViewById(R.id.Logo);
英文:

You didn't matching image with your xml

image = findViewById(R.id.Logo);

instead of

ImageView Logo = findViewById(R.id.Logo);

答案3

得分: 1

你没有初始化 image

在设置 image 的动画之前,你必须先初始化 image

像这样:image = findViewById(R.id.your_responsable_image_id);

当你没有初始化一个视图但想要访问它时,你会得到 NullPointerException

英文:

You did't initialize image

You have to initialize image before setAnimation to image

like image = findViewById(R.id.your_responsable_image_id);

when you not initialize a view and want to access it , Then you will got NullPointerException

huangapple
  • 本文由 发表于 2020年8月6日 10:18:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/63275944.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定