我正在尝试使用 Tabhost,我有 2 个选项卡,每个选项卡加载一个 Activity,每个 Activity 都有一个 aSync 方法来解析一些数据。如果我setCurrentTab(0)
在创建 Tabmenu Activity 时这样做一切正常,但如果我尝试在第二个选项卡上启动应用程序(setCurrentTab(1)
),它会尝试加载第一个选项卡,并执行第一个选项卡的 aSync 方法并崩溃,因为缺少某些数据让方法起作用,根本不应该调用它。
这是 TabMenu.Activity:
public class TabMenu extends TabActivity {
TabHost tabHost = null;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
setResult(resultCode);
this.finish();
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Intent intentToGet = getIntent();
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
TabHost.TabSpec spec2;
Intent intent2;
intent = new Intent().setClass(this, HoursActivity.class);
intent.putExtra("student", intentToGet.getStringExtra("student"));
intent.putExtra("pass", intentToGet.getStringExtra("pass"));
intent.putExtra("un", intentToGet.getStringExtra("un"));
spec = tabHost
.newTabSpec("hours")
.setIndicator("Hours")
.setContent(intent);
tabHost.addTab(spec);
intent2 = new Intent().setClass(this, NotesActivity.class);
intent2.putExtra("student", intentToGet.getStringExtra("student"));
intent2.putExtra("pass", intentToGet.getStringExtra("pass"));
intent2.putExtra("un", intentToGet.getStringExtra("un"));
spec2 = tabHost
.newTabSpec("notes")
.setIndicator("Notes")
.setContent(intent2);
tabHost.addTab(spec2);
tabHost.setCurrentTab(1);
}
}
有任何想法吗?