我有一个 tabHostMainActivity
和两个 A、B 活动。在每个选项卡中,我想将名为 cityName 的字符串从 MainActivity 传输到 A 活动。但我总是NullPointerException
在 Logcat 中得到一个并被迫关闭 TT
这是我的代码:
public class MainActivity extends TabActivity {
private static TabHost tabHost;
public static String selectedCity;
Bundle selectedCityBundle = new Bundle();
Intent selectedCityIntent = new Intent();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Resources res = getResources();
selectedCityBundle.putString("CityName", "LA");
selectedCityIntent.setClass(this, A.class);
selectedCityIntent.putExtras(selectedCityBundle);
tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
intent = new Intent().setClass(this, A.class);
spec = tabHost.newTabSpec("tab1")
.setIndicator("A", res.getDrawable(R.drawable.start))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, B.class);
spec = tabHost.newTabSpec("tab2")
.setIndicator("B", res.getDrawable(R.drawable.weather))
.setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(0);
}
A级
public class A extends Activity {
TextView tempNum, cn1, cn2, cn3, ln1, ln2, ln3, city, rainProb;
Intent cityIntent = new Intent();
Bundle cityBundlecityBundle = cityIntent.getExtras();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start);
tempNum = (TextView) findViewById(R.id.temp);
rainProb = (TextView) findViewById(R.id.raining);
cn1 = (TextView) findViewById(R.id.zone1News1);
cn2 = (TextView) findViewById(R.id.zone1News2);
cn3 = (TextView) findViewById(R.id.zone1News3);
ln1 = (TextView) findViewById(R.id.zone2News1);
ln2 = (TextView) findViewById(R.id.zone2News2);
ln3 = (TextView) findViewById(R.id.zone2News3);
city = (TextView) findViewById(R.id.location);
city.setText(cityBundle.getString("CityName"));
//Always got error NullPointerException here
}
}