Как запустить методы во втором или других действиях в Android? - PullRequest
0 голосов
/ 21 апреля 2020

Я пытаюсь создать игру и могу успешно переключиться на второе действие, где фактически находится моя игра, но когда я нажимаю кнопку, чтобы вызвать метод diceroll во втором действии моего приложения, мое приложение всегда дает сбой, всегда происходит сбой. Пожалуйста, помогите мне, я новичок в Android.

Вот код, и я также приложил свой отчет об ошибке в конце.

package com.example.ludobyjagtar;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class ludogame extends AppCompatActivity {


    int activeplayer=1;
    String namez1="x";
    String namez2="x";
    String namez3="x";
    String namez4="x";

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

        Intent intent = getIntent();
        Bundle extras = intent.getExtras();
         namez1 = extras.getString("username1");
         namez2 = extras.getString("username2");
         namez3 = extras.getString("username3");
         namez4 = extras.getString("username4");
        TextView text =findViewById(R.id.textView8);
        text.setText(namez1+"turn to play");


    }


    String[] unamez={namez1+"turn to play",namez2+"turn to play",namez3+"turn to play",namez4+"turn to play"};



    int x=1000;
    int y=2000;
    int z=3000;
    int a=4000;
    int permission=0;
    int[] gamestate={x,x,x,x,y,y,y,y,z,z,z,z,a,a,a,a,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777,777};
    int[] winpos={74,80,86,92};
    int[] specialpos={63,16,24,29,37,42,50,55};
    int tokencounter=0;
    int tapnumcal=0;
    int dicecurval=0;

    public void diceroll(View view)
    {

        int min=0;
        int max=6;
        int x = (int)(Math.random()*((max-min)+1))+min;
        TextView text =findViewById(R.id.textView7);
        text.setText(x);
        dicecurval=x;
        if(x==6&&activeplayer==0&&(gamestate[0]==x||gamestate[1]==x||gamestate[2]==x||gamestate[3]==x))
        {
            permission=1;



        }
        if(x==6&&activeplayer==1&&(gamestate[4]==y||gamestate[5]==y||gamestate[6]==y||gamestate[7]==y))
        {
            permission=1;


        }
        if(x==6&&activeplayer==2&&(gamestate[8]==z||gamestate[9]==z||gamestate[10]==z||gamestate[11]==z))
        {
            permission=1;


        }
        if(x==6&&activeplayer==3&&(gamestate[12]==a||gamestate[13]==a||gamestate[14]==a||gamestate[15]==a))
        {
            permission=1;


        }

        if(permission==0)
        {
            text=findViewById(R.id.textView8);
            text.setText(unamez[activeplayer+1]);

            if(activeplayer==3)
            {
                activeplayer=0;
            }
            else
            {
                activeplayer=activeplayer+1;
            }

        }





    }

    public void turn(View view)
    {

        ImageView img = (ImageView)view;
        int tapnum=Integer.parseInt(img.getTag().toString());



        if(activeplayer==0)
        {
            if(permission==1)
            {

                if(tapnum==0||tapnum==1||tapnum==2||tapnum==3)
                {
                    ImageView img2 = findViewById(R.id.imageView62);
                    img.setImageResource(0);
                    img2.setImageResource(R.drawable.redtoken);
                    gamestate[16] = x;
                    tokencounter++;
                    gamestate[tapnum] = 777;
                    activeplayer = activeplayer + 1;
                }


                tapnumcal=tapnum+dicecurval;
                ImageView img2= img.findViewWithTag(tapnum);
                img.setImageResource(0);
                img2.setImageResource(R.drawable.redtoken);
            }

        }
        if(activeplayer==1)
        {
            if(permission==1)
            {
                if(tapnum==4||tapnum==5||tapnum==6||tapnum==7)
                {
                    ImageView img2 = findViewById(R.id.imageView16);
                    img.setImageResource(0);
                    img2.setImageResource(R.drawable.greentoken);
                    gamestate[29] = y;
                    gamestate[tapnum] = 777;
                    activeplayer = activeplayer + 1;
                }

                tapnumcal=tapnum+dicecurval;
                ImageView img2= img.findViewWithTag(tapnum);
                img.setImageResource(0);
                img2.setImageResource(R.drawable.greentoken);

            }
        }
        if(activeplayer==2)
        {
            if(permission==1)
            {
                if(tapnum==8||tapnum==9||tapnum==10||tapnum==11)
                {
                    ImageView img2 = findViewById(R.id.imageView89);
                    img.setImageResource(0);
                    img2.setImageResource(R.drawable.yellowtoken);
                    gamestate[42] = y;
                    gamestate[tapnum] = 777;
                    activeplayer = activeplayer + 1;
                }

                tapnumcal=tapnum+dicecurval;
                ImageView img2= img.findViewWithTag(tapnum);
                img.setImageResource(0);
                img2.setImageResource(R.drawable.yellowtoken);

            }
        }
        if(activeplayer==3)
        {
            if(permission==1)
            {
                if(tapnum==12||tapnum==13||tapnum==14||tapnum==15)
                {

                    ImageView img2 = findViewById(R.id.imageView95);
                    img.setImageResource(0);
                    img2.setImageResource(R.drawable.bluetoken);
                    gamestate[55] = y;
                    gamestate[tapnum] = 777;
                    activeplayer = 0;
                }

                //tapnumcal= (String.valueOf(tapnum+dicecurval));
                tapnumcal=tapnum+dicecurval;
                ImageView img2= img.findViewWithTag(tapnum);
                img.setImageResource(0);
                img2.setImageResource(R.drawable.bluetoken);



            }

        }

    }

}

сообщение об ошибке :

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.ludobyjagtar, PID: 21613
    java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:402)
        at android.view.View.performClick(View.java:5637)
        at android.view.View$PerformClick.run(View.java:22429)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6119)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:397)
        at android.view.View.performClick(View.java:5637) 
        at android.view.View$PerformClick.run(View.java:22429) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6119) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5
        at android.content.res.Resources.getText(Resources.java:335)
        at android.widget.TextView.setText(TextView.java:4555)
        at com.example.ludobyjagtar.ludogame.diceroll(ludogame.java:63) ```

1 Ответ

1 голос
/ 21 апреля 2020

Проблема здесь (в diceroll):

text.setText(x);

Ожидается, что целочисленное значение будет представлять идентификатор ресурса строки - отсюда ошибка:

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

Вместо этого выполните :

text.setText(Integer.toString(x));

Просмотрите все ваши setText s на наличие похожих ошибок.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...