我正在尝试使用存储在设备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
答案 0 :(得分:2)
您的p.getTime()
方法会返回int
。将此int
值与setText()
一起使用会使TextView
认为您使用引用string
资源的ID来设置文本,这很可能不是这种情况。而是使用:
time.setText(String.valueOf(p.getTime()));