gpt4 book ai didi

java - 实现图像淡入时基本应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 03:48:20 24 4
gpt4 key购买 nike

我正在尝试开发一个简单的第一个 Android 应用程序,虽然最终它将是一个摇动激活的应用程序,但现在它仍然可以通过单击按钮来工作,因为我需要能够测试它在我拥有的模拟器上。

该应用程序的最终目标是,当设备摇晃时,从三个随机数字中选择一个(每个数字对应于节目中的一个角色)并读取该数字以淡入该角色的图像。紧接着,它会淡入来自该特定字符的引号数组中的随机引号。

目前,我已经让应用程序正常工作,当我单击按钮时,应用程序选择一个字符,并从该字符的数组中选择一个引号,并且能够淡入该引号。为了实现图像淡入,我做了一些让应用程序在我尝试运行时崩溃的事情。

我确信这会是一些愚蠢的错误,但如果能找到它那就太好了。

该项目有四个类文件:MainActivity.java、DoctorWho.java、Nine.javaTen.java>11.java。九、十和十一几乎完全相同,只是使用不同的引号。

在尝试添加图像淡入时,我将其添加到DoctorWho.java,此处:

public class DoctorWho {

private Nine mNine = new Nine();
private Ten mTen = new Ten();
private Eleven mEleven = new Eleven();
private ImageView mImageView1;
private ImageView mImageView2;
private ImageView mImageView3;

int randomNumber = 0;

public String getDoctorQuote() {
String quote = "";

// Choose a Random number out of three values
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(3);

// Use that value to choose which of the Doctors to get a quote from
if (randomNumber == 0) {
// Quote from Nine
quote = mNine.getQuote();
}
else if (randomNumber == 1) {
// Quote from Ten
quote = mTen.getQuote();
}
else if (randomNumber == 2) {
// Quote from Eleven
quote = mEleven.getQuote();
}
else {
quote = "Error";
}
return quote;
}

public void animateDoctor() {
if (randomNumber == 0) {
animateNinthDoctor();
}
else if (randomNumber == 1) {
animateTenthDoctor();
}
else if (randomNumber == 2) {
animateEleventhDoctor();
}
}

private void animateNinthDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView1.setAnimation(fadeInAnimation);
}

private void animateTenthDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView2.setAnimation(fadeInAnimation);
}

private void animateEleventhDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView3.setAnimation(fadeInAnimation);
}

在我的 MainActivity.java 文件中,我尝试在此处单击运行动画:

            @Override
public void onClick(View arg0) {
String quote = mDoctorWho.getDoctorQuote();
mDoctorWho.animateDoctor();
// mQuoteLabel is the TextView where the quotes are displayed
mQuoteLabel.setText(quote);
animateQuoteIn();
}

该应用程序仍然可以正常启动,并且在模拟器上运行它之前不会显示任何错误。当我单击应该运行此序列的按钮时,应用程序崩溃了。控制台中没有显示任何错误,并且我无法在 LogCat View 中找到特定行 - 但我可能找错了地方。让我知道用更多代码更新帖子是否有助于查找导致崩溃的原因。

最佳答案

尝试按如下方式更改 DoctorWho 类:

你还初始化了DoctorWho类中的mImageView1,mImageView2,mImageView3吗???

public class DoctorWho {

private Nine mNine = new Nine();
private Ten mTen = new Ten();
private Eleven mEleven = new Eleven();
private ImageView mImageView1;
private ImageView mImageView2;
private ImageView mImageView3;

int randomNumber = 0;

public String getDoctorQuote() {
String quote = "";

// Choose a Random number out of three values
Random randomGenerator = new Random();
randomNumber = randomGenerator.nextInt(3);//<--- don't create new randonNumber

// Use that value to choose which of the Doctors to get a quote from
if (randomNumber == 0) {
// Quote from Nine
quote = mNine.getQuote();
}
else if (randomNumber == 1) {
// Quote from Ten
quote = mTen.getQuote();
}
else if (randomNumber == 2) {
// Quote from Eleven
quote = mEleven.getQuote();
}
else {
quote = "Error";
}
return quote;
}

public void animateDoctor() {
ImageView imageView=null;
if (randomNumber == 0) {
imageView=mImageView1;
}
else if (randomNumber == 1) {
imageView=mImageView2;
}
else if (randomNumber == 2) {
imageView=mImageView3;
}

AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
imageView.setAnimation(fadeInAnimation);
}

}

关于java - 实现图像淡入时基本应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20502502/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com