Dravic Dravic - 18 days ago 5
Java Question

I've got a very strange crash during Android TextAdventure development

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

The situation:

I press a button that should launch this part of code (work in progress on this, it's not complete yet):

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


I've tested a bit and it seems that if I remove part trying to setText to a TextView inside a table view:

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


It's no longer crashing (but obviously the table will be empty and that's not what I want!

Question: how do I write to such table:

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>

Answer

Based on your stack trace image

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

You will get this Exception, when you are tring to set int value to the TextView, or Edittext or Button, Toast etc views.

When you set the int value to the views, it will consider them as String resources, it will try to search for the string resource with the specified id, and will throw Resources$NotFoundException

Change this

textView.setText(enemyhealth);

to

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