program programmer
program programmer

Reputation: 23

Display method not working properly

enter image description here

I'm creating a 1 on 1 poker chip counter app for my android begginers project. I receive a runtime error when i press any button(+,-,BET/RAISE) which calls displayBetPlayer1 or displayBetPlayer2 so I know the problem is with these methods. I also saw that something's not right with import statements but I'm too new to this to understand what. Can anyone see what's the problem?(I'll post the activity_main.xml in the comments because it doesn't fit the post)

This is the error I receive:

02-14 09:31:35.150 31591-31591/com.example.android.headsuppokercounter E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.headsuppokercounter, PID: 31591 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5697) at android.widget.TextView.performClick(TextView.java:10814) at android.view.View$PerformClick.run(View.java:22526) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7229) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5697)  at android.widget.TextView.performClick(TextView.java:10814)  at android.view.View$PerformClick.run(View.java:22526)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7229)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)  Caused by: android.content.res.Resources$NotFoundException: String resource ID #0xa at android.content.res.Resources.getText(Resources.java:1468) at android.widget.TextView.setText(TextView.java:5040) at com.example.android.headsuppokercounter.MainActivity.displayBetPlayer1(MainActivity.java:551) at com.example.android.headsuppokercounter.MainActivity.incrementP1(MainActivity.java:501) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5697)  at android.widget.TextView.performClick(TextView.java:10814)  at android.view.View$PerformClick.run(View.java:22526)  at android.os.Handler.handleCallback(Handler.java:739)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:158)  at android.app.ActivityThread.main(ActivityThread.java:7229)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

This is MainActivity.java:

package com.example.android.headsuppokercounter;

import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.view.View;
import android.widget.TextView;

import org.w3c.dom.Text;

import static com.example.android.headsuppokercounter.R.id.blinds;
import static com.example.android.headsuppokercounter.R.id.blinds_2;
import static com.example.android.headsuppokercounter.R.id.cash_p_1;
import static com.example.android.headsuppokercounter.R.id.cash_p_2;
import static com.example.android.headsuppokercounter.R.id.pot;
import static com.example.android.headsuppokercounter.R.id.pot_2;
import static com.example.android.headsuppokercounter.R.id.your_turn_p_1;
import static com.example.android.headsuppokercounter.R.id.your_turn_p_2;
import static com.example.android.headsuppokercounter.R.id.bet_text_p_1;
import static com.example.android.headsuppokercounter.R.id.bet_text_p_2;



public class MainActivity extends AppCompatActivity {
    int betAmountP1 = 0;
    int betAmountP2 = 0;
    int whatsInPotOfP1 = 0;
    int pOneCash = 1000;
    int pTwoCash = 1000;
    int whatsInPotOfP2 = 0;
    int smallBlind = 10;
    int bigBlind = smallBlind * 2;
    int potMain = whatsInPotOfP1 + whatsInPotOfP2;
    int blindP1 = smallBlind;
    int blindP2 = bigBlind;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        View decorView = getWindow().getDecorView();
        if (hasFocus) {
            decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                    | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                    | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                    | View.SYSTEM_UI_FLAG_FULLSCREEN
                    | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
            );
        }
    }

    /**
     * This method is called when the RAISE BLINDS button is clicked.
     */
    public void raiseBlinds(View v) {
        if (smallBlind == 10 && bigBlind == smallBlind * 2) {
            smallBlind = 15;
            bigBlind = 30;
        } else if (smallBlind == 15 && bigBlind == smallBlind * 2) {
            smallBlind = 20;
            bigBlind = 40;
        } else if (smallBlind == 20 && bigBlind == smallBlind * 2) {
            smallBlind = 25;
            bigBlind = 50;
        } else if (smallBlind == 25 && bigBlind == smallBlind * 2) {
            smallBlind = 50;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 50 && bigBlind == smallBlind * 2) {
            smallBlind = 75;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 75 && bigBlind == smallBlind * 2) {
            smallBlind = 100;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 100 && bigBlind == smallBlind * 2) {
            smallBlind = 150;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 150 && bigBlind == smallBlind * 2) {
            smallBlind = 200;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 200 && bigBlind == smallBlind * 2) {
            smallBlind = 300;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 300 && bigBlind == smallBlind * 2) {
            smallBlind = 400;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 400 && bigBlind == smallBlind * 2) {
            smallBlind = 500;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 500 && bigBlind == smallBlind * 2) {
            smallBlind = 600;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 600 && bigBlind == smallBlind * 2) {
            smallBlind = 800;
            bigBlind = smallBlind * 2;
        } else if (smallBlind == 800 && bigBlind == smallBlind * 2) {
            smallBlind = 1000;
            bigBlind = smallBlind * 2;
        }
        displayBlinds();
    }


    /**
     * This method is called when the START/RESET button is clicked.
     */
    public void startGame(View v) {
        smallBlind = 10;
        bigBlind = smallBlind * 2;
        displayBlinds();
        blindP1 = smallBlind;
        blindP2 = bigBlind;
        whatsInPotOfP1 = blindP1;
        whatsInPotOfP2 = blindP2;
        displayWhatsInPot1();
        displayWhatsInPot2();
        pOneCash = 1000 - whatsInPotOfP1;
        pTwoCash = 1000 - whatsInPotOfP2;
        displayP1Cash();
        displayP2Cash();
        displayPot();
        displayYourTurnP1();

    }

    /**
     * This method displays pot size of P1.
     */

    public void displayWhatsInPot1() {
        TextView whatIsInPotP1 = (TextView) findViewById(R.id.whats_in_pot_p_1);
        whatIsInPotP1.setText("($" + whatsInPotOfP1 + " in pot)");
    }

    /**
     * This method displays pot size of P2.
     */

    public void displayWhatsInPot2() {
        TextView whatIsInPotP2 = (TextView) findViewById(R.id.whats_in_pot_p_2);
        whatIsInPotP2.setText("($" + whatsInPotOfP2 + " in pot)");
    }

    /**
     * This method displays pot for P1 and P2.
     */

    public void displayPot() {
        potMain = whatsInPotOfP1 + whatsInPotOfP2;
        if (potMain>2000){
            potMain=2000;
        }
        TextView potNumber = (TextView) findViewById(pot);
        potNumber.setText("Pot: $" + potMain);
        TextView pot2Number = (TextView) findViewById(pot_2);
        pot2Number.setText("Pot: $" + potMain);
    }



    /**
     * This method displays what are the current blinds on the screen.
     */
    private void displayBlinds() {
        TextView blindsText = (TextView) findViewById(blinds);
        blindsText.setText("Blinds are $" + smallBlind + "/" + bigBlind);
        TextView blindsText2 = (TextView) findViewById(blinds_2);
        blindsText2.setText("Blinds are $" + smallBlind + "/" + bigBlind);
    }

    /**
     * This method displays YOUR TURN only for P1.
     */
    public void displayYourTurnP1() {
       int toCallP1 = whatsInPotOfP2 - whatsInPotOfP1;
        if(toCallP1==0 && potMain == 2000){
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("");
            yourTurnP1.setBackgroundColor(Color.TRANSPARENT);
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("");
            yourTurnP2.setBackgroundColor(Color.TRANSPARENT);
        }else if(toCallP1>0){
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("$" + toCallP1 + " to call");
            yourTurnP1.setBackgroundColor(Color.GREEN);
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("");
            yourTurnP2.setBackgroundColor(Color.TRANSPARENT);
        }else if(toCallP1 == 0 ){
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("Raise or check");
            yourTurnP1.setBackgroundColor(Color.GREEN);
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("");
            yourTurnP2.setBackgroundColor(Color.TRANSPARENT);
        }
        }


    /**
     * This method displays YOUR TURN only for P1.
     */
    public void displayYourTurnP2() {
        int toCallP2 = whatsInPotOfP1 - whatsInPotOfP2;
        if(toCallP2==0 && potMain == 2000){
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("");
            yourTurnP2.setBackgroundColor(Color.TRANSPARENT);
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("");
            yourTurnP1.setBackgroundColor(Color.TRANSPARENT);
        }else if(toCallP2>0){
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("$" + toCallP2 + " to call");
            yourTurnP2.setBackgroundColor(Color.GREEN);
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("");
            yourTurnP1.setBackgroundColor(Color.TRANSPARENT);
        }else if(toCallP2 == 0 ){
            TextView yourTurnP2 = (TextView) findViewById(your_turn_p_2);
            yourTurnP2.setText("Raise or check");
            yourTurnP2.setBackgroundColor(Color.GREEN);
            TextView yourTurnP1 = (TextView) findViewById(your_turn_p_1);
            yourTurnP1.setText("");
            yourTurnP1.setBackgroundColor(Color.TRANSPARENT);
        }

    }

    /**
     * This method displays all things only for P1.
     */
    public void displayAllP1() {

        displayWhatsInPot1();
        displayWhatsInPot2();
        displayP1Cash();
        displayP2Cash();
        displayYourTurnP2();
        displayPot();
    }

    /**
     * This method displays all things only for P2.
     */
    public void displayAllP2() {
        displayPot();
        displayWhatsInPot1();
        displayWhatsInPot2();
        displayP1Cash();
        displayP2Cash();
        displayYourTurnP1();
    }

    /**
     * This method is called when the All in button of P1 is clicked.
     */
    public void allIn(View v) {
        whatsInPotOfP1 += pOneCash;
        pOneCash = 0;
        potMain = whatsInPotOfP1 + whatsInPotOfP2;
        displayAllP1();
    }

    /**
     * This method is called when the All in button of P1 is clicked.
     */
    public void allIn2(View v) {
        whatsInPotOfP2 += pTwoCash;
        pTwoCash = 0;
        potMain = whatsInPotOfP1 + whatsInPotOfP2;
        displayAllP2();
    }

    /**
     * This method is called when the Check button of P1 is clicked.
     */
    public void check(View v) {

        displayYourTurnP2();

        //Display your turn for P2
    }

    /**
     * This method is called when the Check button of P1 is clicked.
     */
    public void check2(View v) {

        displayYourTurnP1();

        //Display your turn for P2
    }



    /**
     * This method displays total cash of P1.
     */

    public void displayP1Cash() {
        TextView displayP1Cash = (TextView) findViewById(cash_p_1);
        displayP1Cash.setText("$" + pOneCash);
    }

    /**
     * This method displays total cash of P2.
     */

    public void displayP2Cash() {
        TextView displayP2Cash = (TextView) findViewById(cash_p_2);
        displayP2Cash.setText("$" + pTwoCash);
    }

    /**
     * This method is called when the call button of P1 is clicked.
     */

    public void call(View view) {

        pOneCash = pOneCash - (whatsInPotOfP2 - whatsInPotOfP1);
        whatsInPotOfP1 = whatsInPotOfP2;
        displayAllP1();

    }

    /**
     * This method is called when the call button of P2 is clicked.
     */

    public void call2(View view) {

        pTwoCash = pTwoCash - (whatsInPotOfP1 - whatsInPotOfP2);
        whatsInPotOfP2 = whatsInPotOfP1;


        displayAllP2();

    }

/**
 * This method is called when the split pot button of P1 or P2 is clicked.
 */
    public void splitPot(View view){
        pOneCash =potMain/2;
        pTwoCash = potMain/2;
        if (blindP1 == smallBlind) {
            blindP1 = bigBlind;
            blindP2 = smallBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP2();
        } else {
            blindP1 = smallBlind;
            blindP2 = bigBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP1();
        }
        whatsInPotOfP1 = blindP1;
        whatsInPotOfP2 = blindP2;
        pOneCash = pOneCash - whatsInPotOfP1;
        pTwoCash = pTwoCash - whatsInPotOfP2;
        displayP1Cash();
        displayP2Cash();
        displayPot();
        displayWhatsInPot1();
        displayWhatsInPot2();
    }
    /**
     * This method is called when the I won button of P1 is clicked.
     */
    public void wonHandP1(View view) {
        pOneCash = pOneCash + potMain;

        if(potMain==2000) {
            smallBlind = 10;
            bigBlind = smallBlind * 2;
            displayBlinds();
            blindP1 = smallBlind;
            blindP2 = bigBlind;
            whatsInPotOfP1 = blindP1;
            whatsInPotOfP2 = blindP2;
            displayWhatsInPot1();
            displayWhatsInPot2();
            pOneCash = 1000 - whatsInPotOfP1;
            pTwoCash = 1000 - whatsInPotOfP2;
            displayP1Cash();
            displayP2Cash();
            displayPot();
            displayYourTurnP1();
        }else if (blindP1 == smallBlind) {
            blindP1 = bigBlind;
            blindP2 = smallBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP2();
        } else if (blindP1 == bigBlind){
            blindP1 = smallBlind;
            blindP2 = bigBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP1();
        }
        whatsInPotOfP1 = blindP1;
        whatsInPotOfP2 = blindP2;
        pOneCash = pOneCash - whatsInPotOfP1;
        pTwoCash = pTwoCash - whatsInPotOfP2;
        displayP1Cash();
        displayP2Cash();
        displayPot();
        displayWhatsInPot1();
        displayWhatsInPot2();
    }

    /**
     * This method is called when the I won button of P2 is clicked.
     */
    public void wonHandP2(View view) {
        pTwoCash = pTwoCash + potMain;
        if(potMain==2000){
            smallBlind = 10;
            bigBlind = smallBlind * 2;
            displayBlinds();
            blindP1 = smallBlind;
            blindP2 = bigBlind;
            whatsInPotOfP1 = blindP1;
            whatsInPotOfP2 = blindP2;
            displayWhatsInPot1();
            displayWhatsInPot2();
            pOneCash = 1000 - whatsInPotOfP1;
            pTwoCash = 1000 - whatsInPotOfP2;
            displayP1Cash();
            displayP2Cash();
            displayPot();
            displayYourTurnP1();
        } else if (blindP1 == bigBlind){
            blindP1 = smallBlind;
            blindP2 = bigBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP1();
        }else if (blindP1 == smallBlind) {
            blindP1 = bigBlind;
            blindP2 = smallBlind;
            potMain = smallBlind + bigBlind;
            displayYourTurnP2();
        }
        whatsInPotOfP1 = blindP1;
        whatsInPotOfP2 = blindP2;


        pOneCash = pOneCash - whatsInPotOfP1;
        pTwoCash = pTwoCash - whatsInPotOfP2;
        displayP1Cash();
        displayP2Cash();
        displayPot();
        displayWhatsInPot1();
        displayWhatsInPot2();
    }
    /**
     * This method is called when the + button of P2 is clicked.
     */

    public void incrementP2(View view) {


        betAmountP2 = betAmountP2 + 10;
        if (betAmountP2 > pTwoCash) {
            betAmountP2 = pTwoCash;
        }
        displayBetPlayer2();

    }

    /**
     * This method is called when the + button of P2 is clicked.
     */

    public void decrementP2(View view) {


        betAmountP2 = betAmountP2 - 10;
        if (betAmountP2 < 0) {
            betAmountP2 = 0;
        }
        displayBetPlayer2();
    }
    /**
     * This method is called when the + button of P1 is clicked.
     */

    public void incrementP1(View view) {


        betAmountP1 = betAmountP1 + 10;
        if (betAmountP1 > pOneCash) {
            betAmountP1 = pOneCash;
            displayBetPlayer1();
        }else {
            displayBetPlayer1();
        }
    }

    /**
     * This method is called when the + button of P1 is clicked.
     */

    public void decrementP1(View view) {


        betAmountP1 = betAmountP1 - 10;
        if (betAmountP1 < 0) {
            betAmountP1 = 0;
        }
        displayBetPlayer1();
    }
    /**
     * This method is called when the Bet/raise button of P1 is clicked.
     */

    public void betRaiseP1(View view) {
        TextView whatIsInPotP1 = (TextView) findViewById(R.id.whats_in_pot_p_1);
        whatsInPotOfP1 += betAmountP1;
        whatIsInPotP1.setText("($ " + whatsInPotOfP1 + " in pot)");
        betAmountP1 = 0;
        displayBetPlayer1();


    }
    /**
     * This method is called when the Bet/raise button of P2 is clicked.
     */

    public void raise2(View view) {
        TextView whatIsInPotP2 = (TextView) findViewById(R.id.whats_in_pot_p_2);
        whatsInPotOfP2 += betAmountP2;
        whatIsInPotP2.setText("($ " + whatsInPotOfP2 + " in pot)");
        betAmountP2 = 0;
        displayBetPlayer2();


    }


    /**
     * This method displays the bet/raise value of Player 1 on the screen.
     */
    private void displayBetPlayer1(){
        TextView betTextPOne = (TextView) findViewById(R.id.bet_text_p_1);
        betTextPOne.setText(betAmountP1);
    }
    /**
     * This method displays the bet/raise value of Player 2 on the screen.
     */
    private void displayBetPlayer2() {
        TextView betTextPTwo = (TextView) findViewById(R.id.bet_text_p_2);
        betTextPTwo.setText(betAmountP2);
    }
}

Upvotes: 1

Views: 885

Answers (2)

GhostCat
GhostCat

Reputation: 140427

Just read your stack traces carefully: it mentions

android.content.res.Resources$NotFoundException: String resource ID #0xa at

In other words: either a resource isn't there, or you using the wrong id. Simply take this exception name as input for your favorite search engine; and start searching.

Given your comment, you ran into this problem: the point is - the method you are using is overloaded. When you pass a int number, then the argument is interpreted as resource id; but when you pass a string, then that string is simply accepted!

Upvotes: 2

Iulian Popescu
Iulian Popescu

Reputation: 2643

As @GhostCat already mentioned, you are using an inexistent resource. Have a look at the following methods from your code:

private void displayBetPlayer1(){
    TextView betTextPOne = (TextView) findViewById(R.id.bet_text_p_1);
    betTextPOne.setText(betAmountP1);
}
/**
 * This method displays the bet/raise value of Player 2 on the screen.
 */
private void displayBetPlayer2() {
    TextView betTextPTwo = (TextView) findViewById(R.id.bet_text_p_2);
    betTextPTwo.setText(betAmountP2);
}

As you may notice, you are setting the text by using the method TextView.setText(int resId) which takes an id of a string resource and sets that string in your textView, but in your case, that id doesn't exist because it isn't assigned to any resource (it's just a simple integer). What you really should use is TextView.setText(String text) and pass the argument as String.valueOf(betAmountP2).

Upvotes: 0

Related Questions