0

我有一个 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
}

}
4

2 回答 2

0

您当前正在将一个捆绑包放入另一个捆绑包中。像这样传递您的城市名称:

selectedCityIntent.putExtra("CityName", "LA");

你的代码应该运行良好。

编辑:

你也没有捕捉到意图,你只是在你的代码中创建了一个新的。在您的 A 类中,像这样获取捆绑包:

Bundle cityBundle;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.start);
    cityBundle = getIntent().getExtras();
    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"));
}

编辑2:

好的,当您向选项卡提供意图时也会出现问题。您没有包括您的 selectedCityBundle。由于您不必要地使用了太多捆绑包,请参阅下面的代码MainActivity

public class MainActivity extends TabActivity {

private static TabHost tabHost;
public static String selectedCity;    

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Resources res = getResources();
    tabHost = getTabHost();     
    TabHost.TabSpec spec;
    Intent intent;
    intent= new Intent().setClass(this, A.class);
    intent.putExtra("CityName", "LA");
    spec = tabHost.newTabSpec("tab1")
            .setIndicator("A", res.getDrawable(R.drawable.start))
            .setContent(intent);
    tabHost.addTab(spec);

    intent = new Intent().setClass(this, B.class);
    intent.putExtra("CityName", "LA");
    spec = tabHost.newTabSpec("tab2")
            .setIndicator("B", res.getDrawable(R.drawable.weather))
            .setContent(intent);
    tabHost.addTab(spec);


    tabHost.setCurrentTab(0);

}

请注意,我不确定意图和规范是否会像这样重用。如果您在更改选项卡时发现任何错误,请尝试在每个选项卡的不同变量中创建新的意图和规范。

于 2012-11-21T03:59:44.580 回答
0

尝试这个...

  Bundle bundle = getIntent().getExtras();
    String string = bundle.getString("CityName");
 city.setText(string);
于 2012-11-21T04:36:16.250 回答