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

时间:2013-12-10 18:32:21

标签: java android crash imageview fadein

我正在尝试使用一个简单的第一个Android应用程序,虽然最终它将成为一个动态激活的应用程序,但现在它仍然可以点击按钮,因为我需要能够测试它在我的模拟器上。

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

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

我敢肯定这会是一个愚蠢的错误,但如果可以找到它会很棒。

该项目有四个类文件: MainActivity.java ,DoctorWho.java, Nine.java Ten.java Eleven.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视图中找到特定的行 - 尽管我可能在错误的位置查找。如果用更多代码更新帖子有助于找到导致崩溃的原因,请告诉我。

1 个答案:

答案 0 :(得分:0)

尝试更改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);
}

}