onActivityResult返回null

时间:2018-05-31 17:55:55

标签: java android

活动1:

公共类MainActivity扩展了AppCompatActivity {

private EditText editOrt;
private EditText editPLZ;
private EditText editStrasse;
final int REQUEST_CODE = 111;
String adressdaten = "";
TextView abrufenn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    editOrt = findViewById(R.id.editOrt);
    editPLZ = findViewById(R.id.editPLZ);
    editStrasse = findViewById(R.id.editStrasse);
}

public void startSecond(View view) {

    Intent i = new Intent(this, SecondActivity.class);
    String inputOrt = editOrt.getText().toString();
    String inputPLZ = editPLZ.getText().toString();
    String inputStrasse = editStrasse.getText().toString();
    i.putExtra("Ort", inputOrt);
    i.putExtra("PLZ", inputPLZ);
    i.putExtra("Strasse", inputStrasse);

    startActivityForResult(i, REQUEST_CODE);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    if (requestCode == REQUEST_CODE) {
        if (resultCode == Activity.RESULT_OK) {
            Bundle b = data.getExtras();

            String backStrasse    = (String) b.get("strasseback");
            String backPLZ        = (String) b.get("plzback");
            String backOrt        = (String) b.get("ortback");
            adressdaten = " " + backStrasse + " " + backPLZ + " " + backOrt;
        }
    }
}

public void abrufen (View view) {
    abrufenn = findViewById(R.id.textView5);
    abrufenn.setText(adressdaten);
}

}

活动2:

公共类SecondActivity扩展了AppCompatActivity {

private EditText editOrt2;
private EditText editPLZ2;
private EditText editStrasse2;
String ort, plz, strasse;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

    editOrt2 = findViewById(R.id.editOrt2);
    editPLZ2 = findViewById(R.id.editPLZ2);
    editStrasse2 = findViewById(R.id.editStrasse2);

    Intent intent = getIntent();
    Bundle daten = intent.getExtras();

    String ort = (String) daten.get("Ort");
    String plz = (String) daten.get("PLZ");
    String strasse = (String) daten.get("Strasse");

    editOrt2.setText(ort);
    editPLZ2.setText(plz);
    editStrasse2.setText(strasse);

}

public void back(View view) {
    Intent result = new Intent (this, MainActivity.class);
    result.putExtra("strasseback", strasse);
    result.putExtra("plzback", plz);
    result.putExtra("ortback", ort);
    setResult(Activity.RESULT_OK, result);

    finish();

}

}

问题在于按钮" abrufenn"方法" abrufen"只显示" null null null"。我不知道为什么但是,数据不会从活动2转移到活动1,感谢您的帮助! :)

4 个答案:

答案 0 :(得分:0)

没有必要把它等于捆绑。 尝试使用 -  String backStrasse = data.getStringExtra(“strasseback”); 如果问题仍未解决,请使用调试器或上传您的xml文件,以便更好地查看您的代码。

答案 1 :(得分:0)

使用此代码更改您的代码

public void back(View view) {
    Intent result = new Intent(this, MainActivity.class);
    Bundle resultBundle = new Bundle();
    resultBundle.putString("strasseback", strasse);
    resultBundle.putString("plzback", plz);
    resultBundle.putString("ortback", ort);
    result.putExtras(resultBundle);
    setResult(Activity.RESULT_OK, result);

    finish();
}

答案 2 :(得分:0)

您的代码中存在两个问题:

  • Intent设置为setResult时。按Intent intent = new Intent()创建就足够了。

  • 您致电result.putExtra("strasseback", strasse);,然后必须根据参数类型data.getStringExtradata.getIntExtra提取,而不是data.getExtras

    < / LI>

答案 3 :(得分:0)

活动2没问题

活动1我使用那些代码

接收数据

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if ((requestCode == 1) && (resultCode == Activity.RESULT_OK)) {
        editText.setText(data.getStringExtra("result"));
    }
}

创建forresult

    public void chooseDirectoryFilesClick(View view) {
    Intent intent = new Intent(this, ChooseDirectoryActivity.class);
    startActivityForResult(intent, 1);
}