将字符串传递给另一个Activity,然后没有HAPPEN

时间:2014-06-23 15:13:55

标签: java android string

这是我的代码,在这个Android代码中,LogCat中没有任何事情发生。有人帮忙 - _ -

public class Keluarga extends Activity{

protected static final int REQUEST_CODE_FUNCTONE = 100;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keluarga_utama);

    EditText nama           = (EditText)findViewById(R.id.editText1);
    EditText umur           = (EditText)findViewById(R.id.editText2);
    EditText gaji           = (EditText)findViewById(R.id.editText3);
    EditText lain           = (EditText)findViewById(R.id.editText4);
    EditText akun           = (EditText)findViewById(R.id.editText5);


    final String namaString             = nama.getText().toString();
    final String umurString             = umur.getText().toString();
    String gajiString                   = gaji.getText().toString();
    String lainString                   = lain.getText().toString();
    String akunString                   = akun.getText().toString();

    double gajiDouble;
    try {
        gajiDouble =(Double.parseDouble(gajiString));
    } catch (NumberFormatException e) {
        gajiDouble = 0; // your default value
    }
    double lainDouble;
    try {
        lainDouble =(Double.parseDouble(lainString));
    } catch (NumberFormatException e) {
        lainDouble = 0; // your default value
    }

    double akunDouble;
    try {
        akunDouble =(Double.parseDouble(akunString));
    } catch (NumberFormatException e) {
        akunDouble = 0; // your default value
    }
    double totalDouble                  = gajiDouble+lainDouble;
    final String totalString            = Double.toString(totalDouble);
    final String tabunganString         = Double.toString(akunDouble);

Button next = (Button)findViewById(R.id.keluarga_next);
next.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
    // TODO Auto-generated method stub
        Intent intent           = new Intent(getApplicationContext (), Keluarga_output.class);
        intent.putExtra("NAMA", namaString);
        intent.putExtra("umur", umurString);
        startActivityForResult(intent, REQUEST_CODE_FUNCTONE);
    }
});
}

这是我的第二项活动:

public class Keluarga_output extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.keluarga_anak);

    Bundle bun = getIntent().getExtras();
    String namaHasil    = bun.getString("NAMA");
    String umurHasil    = bun.getString("umur");

    TextView txtNama    = (TextView)findViewById(R.id.textviewnama);
    TextView txtUmur    = (TextView)findViewById(R.id.textviewumur);

    txtNama.setText(namaHasil);
    txtUmur.setText(umurHasil);
}
}
}

2 个答案:

答案 0 :(得分:1)

更改

 Bundle bun = getIntent().getExtras();

  Intent i = getIntent();    
  String _one = i.getStringExtra("NAMA");
  String _two = i.getStringExtra("umur");

会给你想要的结果。

答案 1 :(得分:0)

试试这个

Button next = (Button)findViewById(R.id.keluarga_next);
next.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

    String namaString  = nama.getText().toString();
    String umurString  = umur.getText().toString();
    Intent intent  = new Intent(getApplicationContext (), Keluarga_output.class);
    intent.putExtra("NAMA", namaString);
    intent.putExtra("umur", umurString);
    startActivityForResult(intent, REQUEST_CODE_FUNCTONE);
  }
});

<强> Keluarga_output.java

public class Keluarga_output extends Activity {

 @Override
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.keluarga_anak);

 Intent bun = getIntent();
 String namaHasil    = bun.getStringExtra("NAMA");
 String umurHasil    = bun.getStringExtra("umur");

  TextView txtNama    = (TextView)findViewById(R.id.textviewnama);
  TextView txtUmur    = (TextView)findViewById(R.id.textviewumur);

  txtNama.setText(namaHasil);
  txtUmur.setText(umurHasil);
 }
}

}