2013-12-10 52 views
0

我試圖在簡單的第一個Android應用程序上工作,雖然最終它將成爲一個動態激活的應用程序,但現在它仍然可以通過點擊按鈕因爲我需要能夠在我的模擬器上進行測試。實現圖像淡入淡出時的基本應用程序崩潰

該應用程序的最終目標是,當設備被震動時,從三個中選擇一個隨機數(每個對應於來自節目的角色)並讀取該數字以淡入該角色的圖像。直接後,它從一個特定字符的引用中隨機引用。

目前,我得到的應用程序的工作點,當我點擊按鈕,應用程序選擇了一個字符,並從該數組引用該字符,並能夠淡入的報價。試圖實現圖像淡入淡出,我做了一些讓我的應用程序崩潰,當我嘗試運行它。

我敢肯定,這將是一個愚蠢的錯誤,但它會是太棒了,如果它可以被發現。

該項目有四個類文件:MainActivity.java,DoctorWho.java,Nine.javaTen.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視圖中找到特定的行 - 但我可能在錯誤的位置查找。讓我知道如果用更多的代碼更新帖子將有助於找出造成崩潰的原因。

+0

哪裏設置mImageView1,mImageView2,mImageView3? –

回答

0

試着改變你DoctorWho類如下:

也有您初始化mImageView1,mImageView2,mImageView3 DoctorWho類內部???

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); 
} 

}