0

I'm trying to populate a highscores table for my game from data stored on the device's SD card.

Here is my 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;
}
}

Here is my 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>

For some reason when I run my leaderboard it causes a RuntimeException

How can I get this working?


UPDATE

As requested, here's my log:

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
2
  • Is there any reason for setting the content view again after that for loop? Also, always mention what does the exception says(and of course post the stacktrace). Commented Mar 29, 2013 at 13:41
  • Here's my log: pastebin.com/euEWtezZ Commented Mar 29, 2013 at 13:49

1 Answer 1

2

Your p.getTime() method returns an int. Using this int value with setText() will make the TextView think you're setting the text using an id referring a string resource which most likely isn't the case. Instead use:

time.setText(String.valueOf(p.getTime()));
Sign up to request clarification or add additional context in comments.

3 Comments

I've just done this and now I have an IllegalStateException instead... so it still crashes. Here's my new log: pastebin.com/evueGVXB
@Jamesking56 As I said in the comment, remove the line setContentView(highscoresTable);. You add the items to the already present table, there is no reason for setting the content to the same table.
Aha! Thank you very much! I thought I'd have to set it again to 'refresh' the table to show the data. I'm new to Android development so thanks for giving me a hand. :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.