Android TabActivity 如何仅在 TabChange 上启动 Intent



我的代码有问题:

我想用谷歌AppEngine制作一个高分列表,它可以工作。Google AppEngine 返回一个字符串,该字符串被解析并填充在布局中。活动 HighscoreListe 连接到 AppEngine,并将获取此字符串。

我在下面写的Highscore布局的问题在于,当我启动TabLayout时,此活动(HighscoreListe)会多次启动。所以我得到了一个StringIndexOutOfBoundsException。

我想在更改为选项卡 2 时启动 intent2,在更改为 Tab3 时启动 intent3。这样当我点击这个难度时,高分只会在第二或第三难度中加载。

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);

}

}

如果我明白你的意思,你应该在标签主机上设置chage listner:

' mTabHost.setOnTabChangedListener(new OnTabChangeListener() {

        @Override
        public void onTabChanged(String tabId) {
            // TODO Auto-generated method stub
        }
    })`

最新更新