gpt4 book ai didi

java - 我在 Android TextAdventure 开发过程中遇到了一个非常奇怪的崩溃

转载 作者:行者123 更新时间:2023-11-30 03:36:06 26 4
gpt4 key购买 nike

http://scr.hu/0eug/7vny2

情况:

我按下一个应该启动这部分代码的按钮(这方面的工作正在进行中,还没有完成):

public void playerattack1(View view){
setContentView(R.layout.activity_fight_defense);
displaystats();
int attacktype = 1;
int dmgminimum = 10;
int dmgmaximum = 15;
Random rn = new Random();
int range = dmgmaximum - dmgminimum + 1;
int damage = rn.nextInt(range) + dmgminimum;
fight_p_attack(damage, attacktype);
sb.insert(0,"Attack!\n");
textView = (TextView) findViewById(R.id.teststring);
textView.setText(sb);


public void fight_p_attack(int damage, int attacktype){
setContentView(R.layout.activity_fight_defense);
int k9minimum = 1;
int k9maximum = 9;
Random rn = new Random();
int range = k9maximum - k9minimum + 1;
int k9 = rn.nextInt(range) + k9minimum;

switch (attacktype){
case 1:
switch (k9){
case 1: damage *= 2;
enemyhealth -= damage;
case 4: enemyhealth -= damage;
case 7: enemyhealth -= damage;
}
case 2:
switch (k9){
case 2: enemyhealth -= damage;
case 5: damage *= 2;
enemyhealth -= damage;
case 8: enemyhealth -= damage;
}
case 3:
switch (k9){
case 3: enemyhealth -= damage;
case 6: enemyhealth -= damage;
case 9: damage *= 2;
enemyhealth -= damage;
}
}
fight_update();
}

public void fight_update(){
textView = (TextView) findViewById(R.id.health);
textView.setText("Health: "+health+" ");
textView = (TextView) findViewById(R.id.mana);
textView.setText("Mana: "+mana+" ");
textView = (TextView) findViewById(R.id.gold);
textView.setText("Gold: "+gold+" ");
textView = (TextView) findViewById(R.id.respect);
textView.setText("Respect: "+respect);
textView = (TextView) findViewById(R.id.enemyhealthView);
textView.setText(enemyhealth);
}

我已经测试了一下,似乎如果我删除试图将 setText 设置为 TableView 中的 TextView 的部分:

    textView = (TextView) findViewById(R.id.enemyhealthView);
textView.setText(enemyhealth);

它不再崩溃(但显然表格将是空的,这不是我想要的!

问题:如何写入这样的表:

http://scr.hu/0eug/zzqz1

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/papier_1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="top" >

<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="@+id/health"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/mana"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="@+id/respect"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center">
<TableRow android:id="@+id/tabla_cabecera" android:layout_width="wrap_content" android:layout_height="match_parent"></TableRow>
<TableLayout android:id="@+id/tabla_cuerpo" android:layout_height="match_parent" android:layout_width="wrap_content">
<TableRow android:id="@+id/tableRow1" android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/enemyname"></TextView>

</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/health"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/enemyhealthView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" ></TextView>

</TableRow>
<TableRow android:id="@+id/tableRow3" android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/attack"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" ></TextView></TableRow>

<TableRow android:id="@+id/tableRow4" android:layout_width="match_parent" android:layout_height="wrap_content">

<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:text="@string/defense"></TextView>
<TextView android:background="@drawable/cell_shape" android:padding="5dp" android:id="@+id/fightView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" ></TextView>

</TableRow>
</TableLayout>


</LinearLayout>

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="playerattack1"
android:text="@string/playerattack1" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="playerattack2"
android:text="@string/playerattack2" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="playerattack3"
android:text="@string/playerattack3" />

<TextView android:id="@+id/teststring"
android:background="@drawable/cell_shape"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>

</ScrollView>

最佳答案

基于您的堆栈跟踪图像

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

当您尝试将 int 值设置为 TextViewEdittextButton 时,您会遇到此异常, Toast 等浏览量。

当你给 View 设置int值时,它会将它们视为字符串资源,它会尝试搜索具有指定id的字符串资源,并抛出Resources$NotFoundException

改变这个

textView.setText(enemyhealth);

textView.setText(String.valueOf(enemyhealth));

关于java - 我在 Android TextAdventure 开发过程中遇到了一个非常奇怪的崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16624421/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com