我从Activity
获取了一些值,并调用onActivityResult()
,onActivityResult()
中的值,但收到的数据为null
CODE
MainActivity
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId() == R.id.action_settings){
Intent intent = new Intent(MainActivity.this, AddDetailActivity.class);
startActivityForResult(intent, request_code);
}
return true;
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == request_code){
if(resultCode == RESULT_OK){
String name = data.getExtras().getString(AddDetailActivity.NAME_KEY);
String address = data.getExtras().getString(AddDetailActivity.ADDRESS_KEY);
Toast.makeText(MainActivity.this, "Name is : " + name + " Address : " + address, Toast.LENGTH_LONG).show();
}
}
}
这是我从中获取数据的第二个Activity
:
public class AddDetailActivity extends Activity {
public static final String NAME_KEY = "U-name";
public static final String ADDRESS_KEY = "add";
protected EditText name_et, address_et;
protected String put_name, put_address;
protected Button button = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_detail);
name_et = (EditText) this.findViewById(R.id.name_editText);
address_et = (EditText) this.findViewById(R.id.address_editText);
put_name = name_et.getText().toString();
put_address = address_et.getText().toString();
//put_type = null;
button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();
intent.putExtra(NAME_KEY, put_name);
intent.putExtra(ADDRESS_KEY, put_address);
setResult(RESULT_OK, intent);
finish();
}
});
}}
如何解决这个错误,谢谢
答案 0 :(得分:7)
只需在AddDetailActivity.java
这应该在onClick
方法
put_name = name_et.getText().toString();
put_address = address_et.getText().toString();
你的点击方法应该是这样的
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
put_name = name_et.getText().toString();
put_address = address_et.getText().toString();
// put_type = null;
Intent intent = new Intent();
intent.putExtra(NAME_KEY, put_name);
intent.putExtra(ADDRESS_KEY, put_address);
setResult(RESULT_OK, intent);
finish();
}
});
答案 1 :(得分:3)
将您的获取代码移至按钮内
button = (Button) findViewById(R.id.add_button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
put_name = name_et.getText().toString();
put_address = address_et.getText().toString();
Intent intent = new Intent();
intent.putExtra(NAME_KEY, put_name);
intent.putExtra(ADDRESS_KEY, put_address);
setResult(RESULT_OK, intent);
finish();
}
});
同样在onActivityResult
进行以下更改
String name = data.getExtras().getString("U-name");
String address = data.getExtras().getString("add");
答案 2 :(得分:0)
Public static variables and Android activity life cycle management
请参考这一点,当活动B结束时,您正在使用该活动的静态变量。
所以AddDetailActivity.NAME_KEY将无法使用
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == request_code){
if(resultCode == RESULT_OK){
String name = data.getExtras().getString("U-name");
String address = data.getExtras().getString("add");
Toast.makeText(MainActivity.this, "Name is : " + name + " Address : " + address, Toast.LENGTH_LONG).show();
}
}