我的代码有问题:
我想用 Google AppEngine 制作一个高分列表,它可以工作。Google AppEngine 返回一个经过解析并填充到布局中的字符串。活动 HighscoreListe 连接到 AppEngine 并将获取此字符串。
我在下面写的 Highscore 布局的问题是,这个活动(HighscoreListe)在我启动 TabLayout 时启动了多次。所以我得到了一个 StringIndexOutOfBoundsException。
当我更改为 Tab 2 时,我想启动 Intent2,当我更改为 Tab3 时,我想启动 Intent3。这样当我点击这个难度时,只会在第 2 或第 3 难度中加载高分。
public class HighscoreTabLayout extends TabActivity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TabHost tabHost = getTabHost();
System.out.println(MenuMainActivity.getDifficulties());
int diffCount = -1;
System.out.println(diffCount);
TabSpec tabSpec1 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
// setting Title and Icon for the Tab
tabSpec1.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_photos_tab));
Intent intent1 = new Intent(this, HighscoreListe.class);
intent1.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
tabSpec1.setContent(intent1);
System.out.println(diffCount);
TabSpec tabSpec2 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
tabSpec2.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_songs_tab));
Intent intent2 = new Intent(this, HighscoreListe.class);
intent2.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
tabSpec2.setContent(intent2);
System.out.println(diffCount);
TabSpec tabSpec3 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
tabSpec3.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_videos_tab));
Intent intent3 = new Intent(this, HighscoreListe.class);
intent3.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
tabSpec3.setContent(intent3);
System.out.println(diffCount);
TabSpec tabSpec4 = tabHost.newTabSpec(MenuMainActivity.getDifficulties()[++diffCount].getName());
tabSpec4.setIndicator(MenuMainActivity.getDifficulties()[diffCount].getName());//, getResources().getDrawable(R.drawable.icon_videos_tab));
Intent intent4 = new Intent(this, HighscoreListe.class);
intent4.putExtra("name", MenuMainActivity.getDifficulties()[diffCount].getName());
tabSpec4.setContent(intent4);
System.out.println(diffCount);
// Adding all TabSpec to TabHost
tabHost.addTab(tabSpec1);
tabHost.addTab(tabSpec2);
tabHost.addTab(tabSpec3);
tabHost.addTab(tabSpec4);
tabHost.setCurrentTab(1);
}
}