英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论