在android中将值从一个窗口传递到另一个窗口
我想将用户在第一个窗口中插入的值显示到下一个窗口。在android中将值从一个窗口传递到另一个窗口,android,pass-by-value,Android,Pass By Value,我想将用户在第一个窗口中插入的值显示到下一个窗口。 我在第一个窗口中接受用户的体重和身高,我想在第二个屏幕上显示它作为您的体重和身高 我搜索了很多,甚至尝试了一段代码,但在emulator中,我得到了一个强制关闭的错误 第一项活动: public class BMI_Main extends Activity { EditText BMI_weight; public String weight; public void onCreate(Bundle savedInstanceSt
我在第一个窗口中接受用户的体重和身高,我想在第二个屏幕上显示它作为您的体重和身高 我搜索了很多,甚至尝试了一段代码,但在emulator中,我得到了一个强制关闭的错误 第一项活动:
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);
}
}
);
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);
}
第二项活动:
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);
}
}
);
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);
}
因此,请帮助我。据我所知,您在
BMI\u结果中有以下成员定义:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
但是您只能在类初始化后调用findViewById
,因为它是View
类的成员函数,所以将此行更改为:
TextView Weight;
并在setContentView(…)
之后,将这一行添加到onCreate
方法中:
编辑:它说“…就在super.onCreate(…)
”之后,现在它是正确的;) 据我所知,您在BMI\u结果中有以下成员定义:
TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);
但是您只能在类初始化后调用findViewById
,因为它是View
类的成员函数,所以将此行更改为:
TextView Weight;
并在setContentView(…)
之后,将这一行添加到onCreate
方法中:
编辑:它说“…就在super.onCreate(…)
”之后,现在它是正确的;) 您应该重写onCreate()
并且onCreate结尾的令牌错误。您应该重写onCreate()
onCreate结尾的标记错误。您应该在第一个窗口/活动中使用Context.startActivity(Intent-Intent)方法
将要传递给第二个窗口/活动的数据存储在Intent对象中,如:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
并在onCreate方法的第二个屏幕/活动中检索它们,如:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");
您应该在第一个窗口/活动中使用Context.startActivity(Intent-Intent)方法
将要传递给第二个窗口/活动的数据存储在Intent对象中,如:
Intent intent = new Intent();
intent.putExtra("weight", weight);
intent.putExtra("height", height);
this.startActivity(intent);
并在onCreate方法的第二个屏幕/活动中检索它们,如:
Intent intent = getIntent(); // This is the intent you previously created.
int weight = intent.getExtra("weight");
int height = intent.getExtra("height");
我现在不能尝试代码,不管怎样,检查LogCat中错误的堆栈跟踪,你应该找出错误所在(如果可以,调整你发布的代码的格式)。我现在不能尝试代码,不管怎样,检查LogCat中错误的堆栈跟踪,你应该找出错误所在(并调整您发布的代码的格式,如果可以)在编写Context.startActivity(Intent-Intent)和this之后。startActivity(Intent)显示错误,我想调用下一页,因此无法获取它。在编写Context.startActivity(Intent-Intent)和this.startActivity(Intent)之后它显示错误,我想转到下一页,因此我无法获取它。很高兴我能提供帮助。你似乎是新来的堆栈溢出。如果他们用答案旁边的复选标记回答你的问题,不要忘记接受答案。这有助于提高提供帮助者的声誉和你自己的声誉。欢迎访问。很高兴我能提供帮助。你似乎是新来的堆栈溢出。如果他们用答案旁边的复选标记回答你的问题,不要忘记接受答案。这有助于提高那些提供帮助的人的声誉和你自己的声誉。欢迎使用SO。嘿,它现在起作用了,只是我覆盖了我的onCreate方法,它起作用了。谢谢你的帮助……嘿,它现在起作用了,只是我覆盖了我的onCreate方法,还有它起作用了。谢谢你的帮助。。。。