java - 为表动态创建表行

标签 java android

我正在尝试根据设备 SD 卡上存储的数据填充我的游戏的高分表。

这是我的LeaderboardActivity.java:

public class LeaderboardActivity extends Activity {
private Leaderboard leaderboard;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_leaderboard);

    TableLayout highscoresTable = (TableLayout) findViewById(R.id.highscoresTable);
    this.leaderboard = new Leaderboard();

    ArrayList<Player> players = leaderboard.getScores();
    if(!(players==null))
    {
        Collections.sort(players);

        for(Player p:players)
        {
            TableRow newRow = new TableRow(this);
            TextView name = new TextView(this);
            TextView time = new TextView(this);

            name.setText(p.getName());
            name.setGravity(Gravity.CENTER_VERTICAL);
            name.setPadding(15, 0, 15, 0);
            time.setText(p.getTime());
            time.setGravity(Gravity.CENTER_VERTICAL);
            time.setPadding(15, 0, 15, 0);

            newRow.addView(name);
            newRow.addView(time);
            highscoresTable.addView(newRow);
        }
        setContentView(highscoresTable);
    }
    else
    {
        Log.i("DEBUG", "players is equal to null!");
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_leaderboard, menu);
    return true;
}
}

这是我的 XML activity_leaderboard.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/background"
android:orientation="horizontal" >

<TableLayout
    android:id="@+id/highscoresTable"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="15dp" >

    <TableRow
        android:id="@+id/headerRow"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_vertical|fill_horizontal"
        android:showDividers="middle" >

        <TextView
            android:text="NAME"
            android:gravity="center_vertical"
            android:padding="15dp">
        </TextView>

        <TextView
            android:gravity="center_horizontal|center_vertical"
            android:padding="15dp"
            android:text="TIME" />

    </TableRow>

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

        <TextView
            android:text="TEST"
            android:gravity="center_vertical"
            android:paddingLeft="15dp"
            android:paddingRight="15dp">
        </TextView>

        <TextView
            android:gravity="center_horizontal|center_vertical"
            android:paddingLeft="15dp"
            android:paddingRight="15dp"
            android:text="NOT REAL" />

    </TableRow>


</TableLayout>

</LinearLayout>

出于某种原因,当我运行排行榜时,它会导致 RuntimeException

我怎样才能让它工作?

<小时/>

更新

根据要求,这是我的日志:

03-29 13:45:41.136: E/AndroidRuntime(9894): FATAL EXCEPTION: main
03-29 13:45:41.136: E/AndroidRuntime(9894): java.lang.RuntimeException: Unable to start activity ComponentInfo{*.*.*.*/*.*.*.*.LeaderboardActivity}: android.content.res.Resources$NotFoundException: String resource ID #0x77
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.os.Looper.loop(Looper.java:123)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread.main(ActivityThread.java:4627)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at java.lang.reflect.Method.invokeNative(Native Method)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at java.lang.reflect.Method.invoke(Method.java:521)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at dalvik.system.NativeStart.main(Native Method)
03-29 13:45:41.136: E/AndroidRuntime(9894): Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x77
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.content.res.Resources.getText(Resources.java:201)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.widget.TextView.setText(TextView.java:2817)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at *.*.*.*.LeaderboardActivity.onCreate(LeaderboardActivity.java:44)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-29 13:45:41.136: E/AndroidRuntime(9894):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-29 13:45:41.136: E/AndroidRuntime(9894):     ... 11 more

最佳答案

您的p.getTime()方法返回一个int。将此 int 值与 setText() 一起使用将使 TextView 认为您正在使用引用 string< 的 id 设置文本 资源很可能不是这种情况。而是使用:

time.setText(String.valueOf(p.getTime()));

关于java - 为表动态创建表行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15704323/

相关文章:

java - 在不使用 Web 服务器的情况下部署 Java Web 服务

android - Unicode ☰ 汉堡包未在 Android 和 Chrome 中显示

java - AVD错误: Unable to start activity ComponentInfo{. ..} : android. view.InflateException : Binary XML file: Error inflating class android. widget.Spinner

java - 调整 JFrame 大小后组件重新定位,并通过拖动鼠标手动调整右上角大小

java - 无法从 jms activemq 队列中删除消息

Java : Replace a specific char in string to it is index

当应用程序被滑动时,Android 在服务中维护变量值

java - 关于 "Error: Type com.google.firebase.iid.zzav is referenced as an interface from com.google.firebase.messaging.zzd"的想法?

android - OrderByChild() 没有对我的 firebase 数据进行排序

android - imageview 中的进度对话框,直到我从服务器加载图像