我正在尝试制作一个游戏,并且可以成功切换到游戏所在的第二个 Activity ,但是当我在应用程序的第二个 Activity 中单击按钮以调用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) ```
最佳答案
问题在这里(在diceroll
中):
text.setText(x);
期望整数值表示字符串资源id-因此出现错误:
android.content.res.Resources$NotFoundException: String resource ID #0x5 at
而是:
text.setText(Integer.toString(x));
查看您的所有
setText
中是否存在类似错误。