在Android TextAdventure开发期间,我遇到了一个非常奇怪的崩溃

时间:2013-05-18 12:47:23

标签: java android crash textview android-tablelayout

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设置为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>

1 个答案:

答案 0 :(得分:1)

基于您的堆栈跟踪图像

  

android.content.res.Resources $ NotFoundException:字符串资源ID#0x7b

当您要将{int}值设置为TextViewEdittextButtonToast等视图时,您将收到此异常。

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

更改此

textView.setText(enemyhealth);

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