通过选项卡中的Bundle传输变量

时间:2012-11-21 03:55:40

标签: android tabs nullpointerexception bundle

我有一个tabHost MainActivity和两个A,B活动。在每个选项卡中,我想将名为cityName的字符串从MainActivity传输到A活动。但我总是在Logcat中得到一个NullPointerException并被强制关闭T.T

这是我的代码:

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
}

}

2 个答案:

答案 0 :(得分: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);

}

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

答案 1 :(得分:0)

试试这个......

  Bundle bundle = getIntent().getExtras();
    String string = bundle.getString("CityName");
 city.setText(string);