简体   繁体   中英

Application randomly stops responding.

I am new to programming and I'm making a very simple blackjack game with only basic functions. When I run the program on the emulator it runs maybe for one hand, two, sometimes 5 or more but it always stops responding at some stage when i click on one of the three butons. There is a splash screen that runs for three seconds and the there is a thread comming from that activity that starts this menu activity. Could anyone maybe tell why this is happening? It usually happens when I clcik on one of the buttons even though there is no much comput

protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);   
    btDeal = (Button) findViewById(R.id.deal);
    playerCards1 = (TextView) findViewById(R.id.playerCards);
    playerPoints = (TextView) findViewById(R.id.playerPoints);
    dealerCards1 =  (TextView) findViewById(R.id.dealerCard);

    mpBusted= MediaPlayer.create(this, R.raw.busted);
    mpWin = MediaPlayer.create(this, R.raw.win);
    mpShuffling = MediaPlayer.create(this, R.raw.shuffling);
    mpEven = MediaPlayer.create(this, R.raw.even);
    mpHit= MediaPlayer.create(this, R.raw.hit);

    btDeal.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            deal ();

        }
    });  //getTotalDealerCards()
        //getTotalPlayerCards()


    btHit = (Button) findViewById(R.id.hit);
    btHit.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub

            Boolean busted = isBusted();


            if(!busted){
                hitPlayer();
                playerCards1.setText(getPlayerCardsToString());
                if (isBusted()){
                    mpBusted.start();

                }else{
                    playerCards1.setText(getPlayerCardsToString());
                    playerPoints.setText(Integer.toString(getTotalPlayerPoints()));
                }
            }

        }
    });

    btStand = (Button) findViewById(R.id.stand);

    btStand.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            checkWinner();

        }// testValue(getTotalPlayerCards())
    });
}





/***********    Function declarations starts here    **********/





//Sum and return the total points for the dealer cards
public int getTotalDealerPoints(){
    int points = 0;
    int aceFlag = 0; //flag to deal with Aces
    int counter;

    for (counter = 0; counter <= getTotalDealerCards(); counter++){
        if (dealerCards [counter].getCard() + 1 == 1){
               points += 11;
               aceFlag++;               
        }
        else if (dealerCards [counter].getCard() + 1 > 10)
            points += 10;

        else
            points += dealerCards [counter].getCard() + 1;
    }

    do {
        if (points > 21 && aceFlag > 0){
            points -= 10;
            aceFlag--;
        }
    } while (aceFlag>0);

    return points;
}


//Get the total player points deal
public int getTotalPlayerPoints(){
    int points = 0;
    int aceFlag = 0; //flag to deal with Aces
    int counter;

    for (counter = 0; counter <= getTotalPlayerCards(); counter++){
        if (playerCards [counter].getCard() + 1 == 1){
               points += 11;
               aceFlag++;               
        }
        else if (playerCards [counter].getCard() + 1 > 10)
            points += 10;

        else
            points += playerCards [counter].getCard() + 1;
    }

    do {
        if (points > 21 && aceFlag > 0){        
            points -= 10;
            aceFlag--;
        }
    } while (aceFlag>0);

    return points;
}


//Deal function to start hand
public void deal (){

    // If deal is pressed reset all and start over.
    mpShuffling.start();
    totalDealerPoints = 0;
    totalPlayerPoints = 0;
    totalCreatedCards = 0;

    for (int i = 0; i < TOTAL_CARDS; i++){
        dealerCards [i] = null;
        playerCards [i] = null;
        createdCards [i] = null;

    }
// create dealer & player cards and save them to dealer, player and total arrays.       

    for (int dealcounter = 0; dealcounter <=1 ; dealcounter++){

        dealerCards[dealcounter]= createCard();
        addCardToCreatedCards(dealerCards[dealcounter]);        
        playerCards[dealcounter] = createCard();
        addCardToCreatedCards(playerCards[dealcounter]);
    }

    String theCards = getPlayerCardsToString();
    String dealerCard = dealerCards[0].toString();
    String playerpoints= Integer.toString(getTotalPlayerPoints());
    playerCards1.setText(theCards);
    dealerCards1.setText(dealerCard);
    playerPoints.setText(playerpoints);//getTotalPlayerPoints()


    while (getTotalDealerPoints() < 16){
        hitDealer();    
    }

}

// Create card and validate against existing before returning object. public Card createCard(){

    int counter2 = 0;
    int flag = 0;
    int value;
    int suit;

    do {
        flag = 0;
        suit = randomer.nextInt(4);
        value = randomer.nextInt(13);
        // validate against permitted values before creating cards
        while (counter2 <= getTotalPlayerCards()) {
           if (createdCards[counter2].getSuit() == suit && createdCards[counter2].getCard() == value || suit > 3 || suit < 0 || value > 12 || value < 0){
              flag = -1;
           }
           counter2++;
        } 

    } while (flag != 0);

    Card theCard = new Card (suit, value);

    return theCard;     
}




// Add card to the records of created cards
public void addCardToCreatedCards(Card aCard){      
    createdCards [totalCreatedCards] = aCard;
    totalCreatedCards++;
}




// Add a card to dealers cards
public void hitPlayer(){

    //If the hand was started add card, else deal to start hand.

    if (getTotalPlayerCards()+1 != 0){ 
        mpHit.start();
        playerCards [getTotalPlayerCards()+1] = createCard();
        addCardToCreatedCards(playerCards [getTotalPlayerCards()]);
    }
    else
        deal();
}

// Create a new card for the dealer
public void hitDealer(){

    dealerCards [getTotalDealerCards()+1] = createCard();
    addCardToCreatedCards(dealerCards [getTotalDealerCards()]);

}

public String getPlayerCardsToString(){

    String cards = "";
    int total = getTotalPlayerCards();

    if (getTotalPlayerPoints() <=21){
        int counter = 0;

        while (counter <= total){

            cards += playerCards[counter].toString() + "  ";
            counter++;
        }

    return  cards;

    }else {
        int counter=0;
        while (counter <= total){

            cards += playerCards[counter].toString() + "  ";
            counter++;
        }
        return cards;
    }
}


public int getTotalPlayerCards(){
    int initialCount = 0;

    while (playerCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}



public int getTotalDealerCards(){
    int initialCount = 0;

    while (dealerCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}

public int getTotalCreatedCards(){
    int initialCount = 0;

    while (createdCards[initialCount] != null){

        initialCount++;
    }

    return initialCount-1;
}


public Boolean isBusted(){
    Boolean busted = false;

    if (getTotalPlayerPoints()>21){
        busted=true;
        totalDealerPoints = 0;
        totalPlayerPoints = 0;
        mpBusted.start();
        playerPoints.setText("You were busted!!");


        for (int i = 0; i < TOTAL_CARDS; i++){
            dealerCards [i] = null;
            playerCards [i] = null;
            createdCards [i] = null;

        }

    }
    return busted;
}


//Check for winner
public void checkWinner(){

    if (getTotalDealerPoints() <= 21 || getTotalPlayerPoints() <= 21 && !isBusted()){




             if (getTotalDealerPoints() > 21 || getTotalDealerPoints() < getTotalPlayerPoints()){
                 playerPoints.setText("You won!!");
                 mpWin.start();

             }

             else if(getTotalDealerPoints() > getTotalPlayerPoints()){
                 mpBusted.start();
                 playerPoints.setText("You were busted!!");

                 for (int i = 0; i < TOTAL_CARDS; i++){
                      dealerCards [i] = null;
                      playerCards [i] = null;
                      createdCards [i] = null;

                 }

             }

             else{

                 mpEven.start();
                 playerCards1.setText("We have same points!");

            }   



    }
    else {

        deal ();            
    }

}

}

Use the debugger in eclipse to find out where it gets frozen.

Also the android emulator is very slow even with a fast PC.

Try using the low resolution simulators.

open DDMS from the android-sdk\\tools and check which method or thread is taking more time to execute.

Use AsyncTask or Handler when there is a functional(Computational) things running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM