Passing value from one window to the another in android
I w开发者_如何学Pythonant to show the value inserted by user in first window to the next window.
I am accepting the User weight & height in first window and I want to show it on the second screen as Your weight & Height.I search a lot and even tried a code but in emulator m getting forcefully closed error.
First Activity :
public class BMI_Main extends Activity
{
  EditText BMI_weight;
  public String weight;
  public void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bmi_main);
    Button submit =(Button)findViewById(R.id.BMI_submit);
    BMI_weight = (EditText)findViewById(R.id.BMI_EdTx_kg);
    submit.setOnClickListener(new View.OnClickListener() 
    {
  public void onClick(View v) 
  {
    weight = BMI_weight.getText().toString();
    // create a bundle
    Bundle bundle = new Bundle();
    // add data to bundle
    bundle.putString("wt", weight);
    // add bundle to the intent
    Intent intent = new Intent(v.getContext(), BMI_Result.class);
        intent.putExtras(bundle);
    startActivityForResult(intent, 0);
      }
    }   
);
Second Activity :
public class BMI_Result extends Activity 
{
 TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
 public void onCreate(Bundle savedInstanceState) 
 {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bmi_result);
    //get the bundle
    Bundle bundle = getIntent().getExtras();
    // extract the data
    String weight = bundle.getString("wt");
    Weight.setText(weight);    
 }
So please help me for it..
As far as I can see you have the following member definition in BMI_Result:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt); 
But you can only call findViewById after the class was initialized, since it is a member function of the View class, so change this line to:
TextView Weight;
And add this line to the onCreate method right after setContentView(...):
Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
Edit: It said "...right after super.onCreate(...)", now it's correct ;)
You should override onCreate()
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
And the token at the end of onCreate is wrong.
You should use the Context.startActivity(Intent intent) method in your first window/Activity.
Store your data which you want to pass to the second window/Activity in the Intent object, like:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
And retrieve them in the second screen/Activity in the onCreate method, like:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论