2

我从 an 中获取一些值,Activity并在onActivityResult(),onActivityResult()中获取这些值,但收到的数据null CODEMainActivity

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

这个bug怎么解决,谢谢

4

3 回答 3

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();
        }
    });
于 2013-07-02T04:44:59.733 回答
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");
于 2013-07-02T04:45:54.057 回答
0

公共静态变量和Android活动生命周期管理

请参考这个,当活动 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();

                        }
    }
于 2013-07-02T04:48:34.423 回答