0

我想将用户在第一个窗口中插入的值显示到下一个窗口。
我在第一个窗口中接受用户体重和身高,我想在第二个屏幕上将其显示为您的体重和身高。

我搜索了很多,甚至尝试了一个代码,但在模拟器 m 中出现强制关闭错误。

第一个活动:

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);    
 }

所以请帮我..

4

3 回答 3

2

据我所见,您在中具有以下成员定义BMI_Result

TextView Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt); 

但是你只能findViewById在类初始化之后调用,因为它是View类的成员函数,所以把这行改成:

TextView Weight;

并将这一行添加到该onCreate方法之后setContentView(...)

Weight = (TextView)findViewById(R.id.BMI_TxtVw_wt);

编辑:它说“...就在super.onCreate(...)”之后,现在它是正确的;)

于 2011-04-09T08:07:45.943 回答
1

您应该在您的第一个窗口/活动中使用 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");
于 2011-04-09T08:59:30.953 回答
1

您应该覆盖 onCreate()

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

而且onCreate末尾的token是错误的。

于 2011-04-09T08:23:31.607 回答