0

tabHostMainActivityと 2 つの A、B アクティビティがあります。各タブで、cityName という文字列を MainActivity から A アクティビティに転送したいと考えています。しかし、私はいつもNullPointerExceptionLogcat を取得し、閉じることを余儀なくされました 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

これを試して...

  Bundle bundle = getIntent().getExtras();
    String string = bundle.getString("CityName");
 city.setText(string);
于 2012-11-21T04:36:16.250 に答える
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 に答える