多个startActivityForResult()

时间:2016-06-30 08:38:34

标签: java android

我需要在我的应用程序中调用此函数两次,指向两个不同的活动。我为每次通话都有一个唯一的请求代码,但是每次启动第二次活动时,我的应用似乎都会崩溃。

这是我的代码(仅相关部分): MainActivity:

ifconfig

EditMarkerActivity:

//Request Info vars
static final int GET_DETAILS = 1;
static final int EDIT_DETAILS = 2;

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

    mMap.setOnMapClickListener(new GoogleMap.OnMapClickListener() {

        public void onMapClick(LatLng latLng) {
            lat = latLng.latitude;
            lon = latLng.longitude;

            startActivityForResult(new Intent(MapsActivity.this,NewMarkerActivity.class), GET_DETAILS);

        }
    });

    mMap.setOnInfoWindowClickListener(new GoogleMap.OnInfoWindowClickListener() {
        @Override
        public void onInfoWindowClick(Marker marker) {
            current_marker = marker;

            startActivityForResult(new Intent(MapsActivity.this,EditMarkerActivity.class), EDIT_DETAILS);
        }
    });





}
@Override
protected void onActivityResult(int requestCode, int resultCode,
                                Intent data) {
    if (requestCode == GET_DETAILS) {
        if (resultCode == RESULT_OK) {

            String marker_title=data.getStringExtra("title");
            String marker_snippet = data.getStringExtra("snippet");

            addMarker(lat, lon, marker_title, marker_snippet);


            mMap.animateCamera(CameraUpdateFactory.newLatLng(new LatLng(lat, lon)));

        }
    } if (requestCode == EDIT_DETAILS) {
         if (resultCode == RESULT_OK) {    
            String marker_title=data.getStringExtra("title");
            String marker_snippet = data.getStringExtra("snippet");

            current_marker.setTitle(marker_title);
            current_marker.setSnippet(marker_snippet);
          }

        }
    }

NewMarkerActivity:

    public class EditMarkerActivity extends AppCompatActivity {

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

        Button save_btn = (Button)findViewById(R.id.btn_save);
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText editName = (EditText)findViewById(R.id.editName);
                String marker_title = editName.getText().toString();

                EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
                String marker_snippet = editSnippet.getText().toString();



                Intent _result = new Intent();
                _result.putExtra("title", marker_title);
                _result.putExtra("snippet", marker_snippet);
                setResult(Activity.RESULT_OK, _result);
                finish();
            }
        });
    }
}

有任何明显的问题吗?非常感谢帮助或洞察这个问题:)

这是Logcat的输出:

    public class NewMarkerActivity extends AppCompatActivity {

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

        Button save_btn = (Button)findViewById(R.id.btn_save);
        save_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                EditText editName = (EditText)findViewById(R.id.editName);
                String marker_title = editName.getText().toString();

                EditText editSnippet = (EditText)findViewById(R.id.editSnippet);
                String marker_snippet = editSnippet.getText().toString();



                Intent _result = new Intent();
                _result.putExtra("title", marker_title);
                _result.putExtra("snippet", marker_snippet);
                setResult(Activity.RESULT_OK, _result);
                finish();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:0)

原来问题是我没有在清单文件中定义活动。 亲爱的老我,现在我知道检查我的错误!

再次感谢大家,案件已经结案。