2013-05-18 4 views
1

http://scr.hu/0eug/7vny2У меня очень странное крушение во время разработки Android TextAdventure

Ситуация:

я нажимаю кнопку, которая должна начать эту часть кода (работа в процессе по этому вопросу, это еще не полный) :

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 к 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> 
+0

макет, Размещенное active_fight_defense? – Blackbelt

ответ

1

На основе вашего стека трассировки изображения

android.content.res.Resources $ NotFoundException: String ресурсов ID № 0x7b

Вы будете получите это исключение, когда вы выполняете tring, чтобы установить значение int в TextView, или Edittext или Button, Toast и т. д. виды.

Когда вы установите значение INT для представлений, он будет рассматривать их как строковые ресурсы, он будет пытаться искать строки ресурса с указанным идентификатором, и выбросит Resources$NotFoundException

Изменить этот

textView.setText(enemyhealth); 

в

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

Хорошо, спасибо, я попробую ... но как бы вы объяснили, что эта часть работает? – Dravic

+0

Goddamnit не может добавить код здесь. – Dravic

+0

, потому что остальные строки. enemyhealth - целое число. Android будет искать строку со значением этого вражеского здоровья – Blackbelt

Смежные вопросы