【问题标题】:Android TabActivity how to start Intent only on TabChangeAndroid TabActivity 如何仅在 TabChange 上启动 Intent
【发布时间】:2012-11-21 12:50:21
【问题描述】:

我的代码有问题:

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

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

我想在切换到 Tab 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);


}

}

【问题讨论】:

    标签: android android-intent tabactivity tabview


    【解决方案1】:

    如果我明白你的意思,你应该在 tabhost 上设置 tab chage listner:

    ` mTab​​Host.setOnTabChangedListener(new OnTabChangeListener() {

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-06-21
      • 2016-08-20
      相关资源
      最近更新 更多