1

I want to mark a location on a new map in a new activity. This is my code.

Ive sent my coordinates with putExtra

Intent intent = new Intent(this, RealMap.class);

Bundle args = new Bundle();
args.putDouble("latitude",honey.latitude);
args.putDouble("longitude",honey.longitude);

intent.putExtra("bundle",args);
startActivity(intent);

Intent i = new Intent(getApplicationContext(),ShowHoney.class);
startActivity(i);

And I received them like this

double lat = 0;
double lon = 0;

Bundle bundle = getIntent().getParcelableExtra("bundle");

if (bundle != null) {
    lat = bundle.getParcelable("latitude");
    lon = bundle.getParcelable("longitude");


    LatLng honey = new LatLng(lat, lon);
    BitmapDescriptor icon = BitmapDescriptorFactory.fromResource(R.mipmap.mylocation);
}

This isn't the full code I don't think there's a problem elsewhere. My code doesn't have problem compiling but when I try it on my phone it shuts down. It says the bundle is a null pointer. I don't know why it won't work.

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Parcelable android.os.Bundle.getParcelable(java.lang.String)' on a null object reference

So I tried putting in if(bundle!=null) The app works, but it doesn't do what I really want it to, because the bundle is always null

Tommy
  • 169
  • 4
  • 16
윤연경
  • 137
  • 1
  • 2
  • 10

4 Answers4

1

Do like this:

Your PutExtra

Intent intent = new Intent(this, RealMap.class);

intent.putExtra("latitude",honey.latitude);
intent.putExtra("longitude",honey.longitude);

startActivity(intent);

Receive them like this

double lat=0;
double lon=0;

Bundle bundle = getIntent().getExtras();

if (bundle != null) {
    lat = bundle.getDouble("latitude");
    lon = bundle.getDouble("longitude");
}
prashant17
  • 1,520
  • 3
  • 14
  • 23
1

Try this

if(getIntent().hasExtra("bundle")){

Bundle bundle = getIntent().getBundleExtra("bundle");
       if(bundle!=null){
         lat = bundle.getDouble("latitude");
         lon = bundle.getDouble("longitude");
    }
}

OR

Send it like this

Intent intent = new Intent(this,ShowHoney.class);
        intent.putExtra("latitude", honey.latitude);
        intent.putExtra("longitude", honey.longitude);
        startActivity(intent);

and recieve like this

if (getIntent().getExtras() != null) {
            Double lat = getIntent().getDoubleExtra("latitude", 0.0);
            Double lon = getIntent().getDoubleExtra("longitude", 0.0);
        }
AbhayBohra
  • 2,047
  • 24
  • 36
0

You are passing Double value and using getParcelable() at receiver activity. That is wrong. You should get Double value by using getDoubleExtra().

    double lat = 0;
    double lon = 0;
    lat = getIntent().getDoubleExtra("latitude",0); // default value 0, if 'latitude' is not passed.
    lon = getIntent().getDoubleExtra("longitude",0);
    LatLng honey = new LatLng(lat, lon);

or by using Bundle

double lat=0;
double lon=0;

Bundle bundle = getIntent().getBundleExtra("bundle");

if (bundle != null) {
    lat = bundle.getDouble("latitude",0);
    lon = bundle.getDouble("longitude",0);
}

This will resolve your problem.

Suggestion:

(1) Also Activity has default putExtra() method so you don't need to use Bundle object. Just use below code.

    Intent intent = new Intent(this, RealMap.class);
    intent.putExtra("latitude", honey.latitude);
    intent.putExtra("longitude", honey.longitude);
    startActivity(intent);

(2) Create constant string keys for putExtra() and getExtra().

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

Bundle b= getIntent().getExtras();

get bundle using this code and check if it is null or not.

MIkka Marmik
  • 1,101
  • 11
  • 29