当前位置: 首页 > 知识库问答 >
问题:

显示方法不能正常工作

江嘉悦
2023-03-14

我正在为我的android乞丐项目创建一个1对1扑克芯片计数器应用程序。当我按下任何调用displayBetPlayer1或displayBetPlayer2的按钮(+、-、Bet/Rise)时,我会收到一个运行时错误,所以我知道问题出在这些方法上。我还看到导入语句有一些不对劲的地方,但我对此太陌生,不明白是什么。有人知道问题出在哪里吗?(我将在注释中发布activity_main.xml,因为它不适合发布)

这是我收到的错误:

02-14 09:31:35.150 315 91-31591/com.example.android.headsuppokercounter e/androidruntime:致命异常:main process:com.example.android.headsuppokercounter,pid:31591 java.lang.IllegalStateException:无法执行Android方法:onclick at android.support.v7.app.appcompatviewWinflater$declaredonClickListener.onclick(appcompatviewWinflater:293)at(view.java:22526)在Android.os.handler.handleCallback(handler.java:739)在Android.os.handler.dispatchMessage(handler.java:95)在Android.os.handler.dispatchMessage(handler.java:158)在Android.os.handler.dispatchMessage(handler.java:158)在Android.app.activitythread.main(activitythread.java:7229)在java.lang.reflect.java:7229)在1120)由:java.lang.reflect.InvocationTargetException在j处引起 Android.view.view.piew.performClick(view.java:5697)的ava.lang.reflect.Method.invoke(本机方法)(android.support.v7.app.appcompatviewWinflater$DeclaredonClickListener.onclick(appcompatviewWinflater.java:288)在Android.widget.textview.performClick(textview.java:10814) 在Android.view.view$performclick.run(view.java:22526) 在Android.os.handler.HandleCallback(handler.java:739) 在Android.os.handler.DispatchMessage(Handler.java:95) 在Android.os.looper.loop(looper.java:158) 在Android.app.activitythread.main(activitythread.java:7229) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:1120) 由: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在Android.support.v7.app.appcompatviewinflater$DeclaredonClickListener.onClick(appcompatviewWinflater.java:288) 在Android.view.view.performClick(view.java:5697) 在Android.widget.textview.performClick(textview.java:10814) 在Android.view.view$performclick.run(view.java:22526) 在Android.os.handler.HandleCallback(handler.java:739) 在Android.os.handler.DispatchMessage(Handler.java:95) 在Android.os.looper.loop(looper.java:158) 在Android.app.activitythread.main(activitythread.java:7229) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:1230) 在com.android.internal.os.zygoteinit.main(zygoteinit.java:1120)

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

共有1个答案

松骏俊
2023-03-14

只需仔细阅读堆栈跟踪:它提到

Android.content.res.resources$NotFoundException:字符串资源ID#0xa位于

换句话说:要么资源不在那里,要么您使用了错误的ID。只需将此异常名称作为您喜爱的搜索引擎的输入;开始搜索。

 类似资料:
  • 我试图用Spring Boot和ReactJs构建一个CRUD应用程序,但我在“Edit”方法中遇到了一些错误。当我试图编辑一个用户时,我在网络选项卡中得到一个404错误,我设法在框中写入,当我想保存而不是编辑我选择的用户时,一个新的添加。“add”方法工作正常,但我认为这是方法之间的重叠。我将把代码留在这里:

  • 我正在尝试将约束移到底部。UI视图的常量,其中我的UITextField显示后位于键盘上方。我设置了通知观察器,但由于某种原因,它不起作用。 当我打印

  • 问题内容: 我正在尝试Java线程方法的示例。但是我发现即使线程已经启动,该方法仍在返回。有人可以告诉我我在做什么错吗?这是代码片段。 问题答案: 如果我的记忆很好,那么java在线程切换之间会有很长的时间间隔,因此isAlive可能会失败,因为线程 尚未 激活。尝试在thread.start()和thread.isAlive()之间添加一些等待时间

  • 我试图在一个变量中保存得分为80分或80分以上的学生的姓名,但我无法使用filter进行保存,它返回整个对象,尽管我指定只打印这些对象的键值,即这些学生的姓名。 我的代码: 我怎样才能得到得分在80分以上的学生的名字?

  • 我正试图在按下某个按钮时弹出一个警报对话框。我首先使用了Android Developer的示例代码而不是'这不起作用,所以我根据在这个站点上发现的情况进行了更改,但是现在我的程序在按下按钮后被迫停止。 就你的知识而言,这是在第二个不同于主要的活动中完成的。不确定这是否重要.... ‘ 碰撞日志:“03-25 19:34:24.373:E/AndroidRuntime(18828):致命异常:ma

  • 2,错误{org.apache.directory.server.LDAP.ldapserver}-ERR_171无法将LDAP服务(10,389)绑定到服务注册表。java.net.BindException:已在使用的地址 请帮忙谢谢 --------提示------------------- JAVA_HOME环境变量设置为/opt/java CARBON_HOME环境变量设置为/mnt/1