onResume()不按预期用于Maps V2

时间:2016-12-04 01:06:49

标签: android google-maps android-fragments android-maps-v2

所以,我一直想要一个简单的"任务管理器应用程序工作,但我遇到了onResume()方法的糟糕情况。我想按下一个名为" use_this_location的按钮后,抓住用户输入的地址的第一行。"当我覆盖onResume()方法时,我尝试将用户拥有的地址文本设置为TextView,但onResume()上的地址似乎总是为空,我相信它是因为该方法在我所期待的不同时间。

我的措辞可能不合适,但我希望我能很好地解释这一情况。

AddLocationMapActivity:地址在mapCurrentAddress()中设置,我使用setOnClickListener()为useLocationButton在setUpViews()中设置新的Intent。

public class AddLocationMapActivity extends FragmentActivity implements OnMapReadyCallback {

    public static final String ADDRESS_RESULT = "address";

    private GoogleMap mMap;
    private UiSettings mUiSettings;
    private Button mapLocationButton;
    private Button useLocationButton;
    private EditText addressText;
    private Address address;


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

        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
    }

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

        // Add zoom buttons
        mUiSettings.setZoomControlsEnabled(true);
        setUpViews();
    }

    private void setUpViews() {
        addressText = (EditText) findViewById(R.id.task_address);
        mapLocationButton = (Button) findViewById(R.id.map_location_button);
        useLocationButton = (Button) findViewById(R.id.use_this_location_button);
        useLocationButton.setEnabled(false);

        useLocationButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (address != null) {
                    Intent intent = new Intent();
                    intent.putExtra(ADDRESS_RESULT, address);
                    setResult(RESULT_OK, intent);
                }

                finish();
            }
        });
        mapLocationButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mapCurrentAddress();
            }
        });
    }

    protected void mapCurrentAddress() {
        String location = addressText.getText().toString();
        List<Address> addresses;

        if (location != null || !location.equals("")) {
            Geocoder geocoder = new Geocoder(this);
            try {
                addresses = geocoder.getFromLocationName(location, 1);
                if (addresses.size() > 0) {
                    address = addresses.get(0);
                    LatLng latLng = new LatLng(address.getLatitude(), address.getLongitude());
                    CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
                    CameraUpdate zoom = CameraUpdateFactory.zoomTo(12);
                    mMap.moveCamera(center);
                    mMap.addMarker(new MarkerOptions().position(latLng).title("Marker"));
                    mMap.animateCamera(zoom);
                    useLocationButton.setEnabled(true);
                } else {
                    // show the user a note that we failed to get an address
                }

            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

AddTaskActivity:在onResume()方法中,我尝试将TextView的文本设置为用户为AddLocationMapActivity键入的地址的第一行,但由于某种原因,地址始终为NULL。如果有人能帮我辨认我的错误,我真的很感激。我可能会误用onResume(),或者我可能会以错误的方式抓取地址。

public class AddTaskActivity extends TaskManagerActivity {
    private static final int REQUEST_CHOOSE_ADDRESS = 0;

    private EditText taskNameEditText;
    private Button addButton;
    private Button cancelButton;
    private boolean changesPending;
    private AlertDialog unsavedChangesDialog;
    private Button addLocationButton;
    private Address address;
    private TextView addressText;

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

    @Override
    protected void onResume() {
        super.onResume();
        if (address == null) {
            addLocationButton.setVisibility(View.VISIBLE);
            addressText.setVisibility(View.GONE);
            //addressText.setVisibility(View.VISIBLE);
            //addressText.setText("ADDRESS ALWAYS NULL");
        } else {
            addLocationButton.setVisibility(View.GONE);
            addressText.setVisibility(View.VISIBLE);
            addressText.setText(address.getAddressLine(0));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_CHOOSE_ADDRESS && requestCode == RESULT_OK) {
            address = data.getParcelableExtra(AddLocationMapActivity.ADDRESS_RESULT);
        } else {
            super.onActivityResult(requestCode, resultCode, data);
        }
    }

    private void setUpViews() {
        taskNameEditText = (EditText) findViewById(R.id.task_name);
        addButton = (Button) findViewById(R.id.add_button);
        cancelButton = (Button) findViewById(R.id.cancel_button);
        addLocationButton = (Button) findViewById(R.id.add_location_button);
        addressText = (TextView) findViewById(R.id.address_text);

        addButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                addTask();
            }
        });
        cancelButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cancel();
            }
        });
        addLocationButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(AddTaskActivity.this, AddLocationMapActivity.class);
                startActivityForResult(intent, REQUEST_CHOOSE_ADDRESS);
            }
        });
        taskNameEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                changesPending = true;
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    protected void addTask() {
        String taskName = taskNameEditText.getText().toString();
        if (!taskName.equals("")) {
            Task t = new Task(taskName);
            getTaskManagerApplication().addTask(t);
        }
        finish();
    }

    public void addLocationButtonClicked(View view) {
        Intent intent = new Intent(this, AddLocationMapActivity.class);
        startActivityForResult(intent, REQUEST_CHOOSE_ADDRESS);
    }

    protected void cancel() {
        String taskName = taskNameEditText.getText().toString();
        if (changesPending && !taskName.equals("")) {
            unsavedChangesDialog = new AlertDialog.Builder(this)
                    .setTitle(R.string.unsaved_changes_title)
                    .setMessage(R.string.unsaved_changes_message)
                    .setPositiveButton(R.string.add_task, new AlertDialog.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            addTask();
                        }
                    })
                    .setNeutralButton(R.string.discard, new AlertDialog.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            finish();
                        }
                    })
                    .setNegativeButton(android.R.string.cancel, new AlertDialog.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            unsavedChangesDialog.cancel();
                        }
                    })
                    .create();
            unsavedChangesDialog.show();
        } else {
            finish();
        }
    }
}

0 个答案:

没有答案