1

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>
4

1 回答 1

1

基于您的堆栈跟踪图像

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

当您尝试将 int 值设置为TextView、 或EdittextButtonToast视图时,您将收到此异常。

当你给views设置int值时,它会将它们视为String资源,它会尝试搜索指定id的string资源,并抛出Resources$NotFoundException

改变这个

textView.setText(enemyhealth);

textView.setText(String.valueOf(enemyhealth));
于 2013-05-18T12:50:56.223 回答