値を追加してのsetTextに挿入

リチャードNicson:

こんにちは私はゲームを作成していますし、それは私が追加とのsetTextで変更されているポイントを作るために必要なスコアを持っています。

私はトラブル私は文字列内のテキスト値を取得するために変換し、それがエラーを与えているの加算を持っています。

    TextView pvoce = (TextView) findViewById(R.id.pvoce);
        TextView papp = (TextView) findViewById(R.id.papp);

if (escolhaApp == "pedra" && escolhaUsuario == "tesoura" || escolhaApp == "papel" && escolhaUsuario == "pedra" || escolhaApp == "tesoura" && escolhaUsuario == "papel") {
            textoResultado.setText("    You Lose   ");
            String soma = 1;
            int totalapp = (soma + papp);
            String totalappString = Integer.toString(totalapp);
            papp.setText(totalappString);

        } else if (escolhaUsuario == "pedra" && escolhaApp == "tesoura" || escolhaUsuario == "papel" && escolhaApp == "pedra" || escolhaUsuario == "tesoura" && escolhaApp == "papel") {
            textoResultado.setText("   You Win   ");
            int soma = 1;
            int totalvoce = soma + pvoce;
            String totalvoceString = Integer.toString(totalvoce);
            pvoce.setText(totalvoceString);
        }

エラー:互換性のない型:intは文字列エラーに変換することができない。互換性のない型:文字列intエラーに変換することができない。INT第二のタイプ:のTextView悪いオペランドタイプバイナリ演算子「+」第一のタイプのために

ザイン:

それはすでに以前の回答でカバーだと私はエラーに多くのストレスを持っていません。しかし、ここでは、問題のいくつかの説明に問題があるコードのあなたの行をコメントアウトし、直接以下の各ラインの修正コードを書くことにより、正しい解決策を見つけることができます

TextView pvoce = (TextView) findViewById(R.id.pvoce);
TextView papp = (TextView) findViewById(R.id.papp);

if (escolhaApp == "pedra" && escolhaUsuario == "tesoura" || escolhaApp == "papel" && escolhaUsuario == "pedra" || escolhaApp == "tesoura" && escolhaUsuario == "papel") {
            textoResultado.setText("    You Lose   ");
            // String soma = 1; // wrong as java is statically typed language, you can't assign a int into a String 
            int soma = 1;

           //  int totalapp = (soma + papp); // papp is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
            int totalapp = soma + Integer.parseInt(papp.getText().toString());

            String totalappString = Integer.toString(totalapp);
            papp.setText(totalappString);

        } else if (escolhaUsuario == "pedra" && escolhaApp == "tesoura" || escolhaUsuario == "papel" && escolhaApp == "pedra" || escolhaUsuario == "tesoura" && escolhaApp == "papel") {
            textoResultado.setText("   You Win   ");
            int soma = 1;
           //  int totalvoce = soma + pvoce; // pvoce is of TextView type, you need to get its text by getText(), convert it to String by .toString(), and then into int to make the integer addition valid
            int totalvoce = soma + Integer.parseInt(pvoce.getText().toString());

            String totalvoceString = Integer.toString(totalvoce);
            pvoce.setText(totalvoceString);
        }

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=315247&siteId=1