0

Hi, I am trying to send data from one activity to another activity using this method: how to retrieve a Double value from one activity to another? Yet every time I open up my application it crashes as its because my code is in the onCreate:

double lat, longi;
Intent getLocation;
Bundle extras;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
getLocation = this.getIntent();
extras = getLocation.getExtras();
lat = extras.getDouble("lat");
longi = extras.getDouble("longi");

but when I put it in a button instead It can't resolve this.getIntent();

public void getCoordinates(View view){
    Button coordinates = (Button) findViewById(R.id.getCoordinates);
    coordinates.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View view){
            getLocation = this.getIntent();
            extras = getLocation.getExtras();
            lat = extras.getDouble("lat");
            longi = extras.getDouble("longi");
        }
    });
}

I would like to receive data either automatically or using a button. I am quite new in mobile computing so please don't roast me.

Konradst
  • 9
  • 5
  • Hi Konradst - I don't think anyone is going to roast you :-). On the button event, this.getIntent() is not resolving due to lexical scoping ("this" is nested in a function and can't see the getIntent() method). I am not an expert in Android development, but to help get a valid answer, it will be better for you to edit the question with only the onCreate function and provide the details of the error you are seeing (e.g., crash report details, error message, etc.). – Jonathan Harrison Dec 05 '17 at 00:32

1 Answers1

0

this meant isView.OnClickListener() in your code .

And we need YourActivity.this as Context

You should use

public void getCoordinates(View view){
Button coordinates = (Button) findViewById(R.id.getCoordinates);
coordinates.setOnClickListener(new View.OnClickListener(){
    @Override
    public void onClick(View view){
        getLocation = YourActivity.this.getIntent();
        extras = getLocation.getExtras();
        lat = extras.getDouble("lat");
        longi = extras.getDouble("longi");
    }
});
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42