App is crashing when calling moveCamera Google maps
I am trying to call moveCamera and my App is crashing. I am receiving a latlng from an Intent extra. I know the extra is coming throungh because I print it updates an edittext. I have tried entering different coordinates myself and nothingis working. Here is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmitm, PID: 28348
java.lang.RuntimeException: Unable to resume activity {com.example.mmitm/com.example.mmitm.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.mmitm.MapActivity.receiveData(MapActivity.java:136)
at com.example.mmitm.MapActivity.onResume(MapActivity.java:111)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
And here is my map activity where I call the movecamera
//onResume Method check which fragment intent is sent from
@Override
protected void onResume() {
super.onResume();
//make sure extras are not null
Bundle extras = getIntent().getExtras();
if (extras != null) {
this.receiveData();
Toast.makeText(this, "Received Data", Toast.LENGTH_SHORT).show();
}
}
private void receiveData() {
//RECEIVE DATA VIA INTENT
Intent i = getIntent();
if(i != null)
{
LatLng locationOne = i.getParcelableExtra("LOC_ONE");
LatLng locationTwo = i.getParcelableExtra("LOC_TWO");
Toast.makeText(this, "LocOne = " + locationOne + ", LocTwo = " + locationTwo, Toast.LENGTH_LONG).show();
//SET DATA TO TEXTVIEWS
locOne.setText(locationOne.toString());
locTwo.setText(locationTwo.toString());
// move camera to location one
Log.d(TAG, "receiveData: calling moveCamera");
//Map Crashes when trying to move camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationOne,15));
}
}
And my MoveCamera function
private void moveCamera(LatLng latLng, float zoom){
Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
This is my first android app so any help is appreciated!
android google-maps-android-api-2
add a comment |
I am trying to call moveCamera and my App is crashing. I am receiving a latlng from an Intent extra. I know the extra is coming throungh because I print it updates an edittext. I have tried entering different coordinates myself and nothingis working. Here is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmitm, PID: 28348
java.lang.RuntimeException: Unable to resume activity {com.example.mmitm/com.example.mmitm.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.mmitm.MapActivity.receiveData(MapActivity.java:136)
at com.example.mmitm.MapActivity.onResume(MapActivity.java:111)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
And here is my map activity where I call the movecamera
//onResume Method check which fragment intent is sent from
@Override
protected void onResume() {
super.onResume();
//make sure extras are not null
Bundle extras = getIntent().getExtras();
if (extras != null) {
this.receiveData();
Toast.makeText(this, "Received Data", Toast.LENGTH_SHORT).show();
}
}
private void receiveData() {
//RECEIVE DATA VIA INTENT
Intent i = getIntent();
if(i != null)
{
LatLng locationOne = i.getParcelableExtra("LOC_ONE");
LatLng locationTwo = i.getParcelableExtra("LOC_TWO");
Toast.makeText(this, "LocOne = " + locationOne + ", LocTwo = " + locationTwo, Toast.LENGTH_LONG).show();
//SET DATA TO TEXTVIEWS
locOne.setText(locationOne.toString());
locTwo.setText(locationTwo.toString());
// move camera to location one
Log.d(TAG, "receiveData: calling moveCamera");
//Map Crashes when trying to move camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationOne,15));
}
}
And my MoveCamera function
private void moveCamera(LatLng latLng, float zoom){
Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
This is my first android app so any help is appreciated!
android google-maps-android-api-2
add a comment |
I am trying to call moveCamera and my App is crashing. I am receiving a latlng from an Intent extra. I know the extra is coming throungh because I print it updates an edittext. I have tried entering different coordinates myself and nothingis working. Here is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmitm, PID: 28348
java.lang.RuntimeException: Unable to resume activity {com.example.mmitm/com.example.mmitm.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.mmitm.MapActivity.receiveData(MapActivity.java:136)
at com.example.mmitm.MapActivity.onResume(MapActivity.java:111)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
And here is my map activity where I call the movecamera
//onResume Method check which fragment intent is sent from
@Override
protected void onResume() {
super.onResume();
//make sure extras are not null
Bundle extras = getIntent().getExtras();
if (extras != null) {
this.receiveData();
Toast.makeText(this, "Received Data", Toast.LENGTH_SHORT).show();
}
}
private void receiveData() {
//RECEIVE DATA VIA INTENT
Intent i = getIntent();
if(i != null)
{
LatLng locationOne = i.getParcelableExtra("LOC_ONE");
LatLng locationTwo = i.getParcelableExtra("LOC_TWO");
Toast.makeText(this, "LocOne = " + locationOne + ", LocTwo = " + locationTwo, Toast.LENGTH_LONG).show();
//SET DATA TO TEXTVIEWS
locOne.setText(locationOne.toString());
locTwo.setText(locationTwo.toString());
// move camera to location one
Log.d(TAG, "receiveData: calling moveCamera");
//Map Crashes when trying to move camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationOne,15));
}
}
And my MoveCamera function
private void moveCamera(LatLng latLng, float zoom){
Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
This is my first android app so any help is appreciated!
android google-maps-android-api-2
I am trying to call moveCamera and my App is crashing. I am receiving a latlng from an Intent extra. I know the extra is coming throungh because I print it updates an edittext. I have tried entering different coordinates myself and nothingis working. Here is my error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mmitm, PID: 28348
java.lang.RuntimeException: Unable to resume activity {com.example.mmitm/com.example.mmitm.MapActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3645)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.GoogleMap.moveCamera(com.google.android.gms.maps.CameraUpdate)' on a null object reference
at com.example.mmitm.MapActivity.receiveData(MapActivity.java:136)
at com.example.mmitm.MapActivity.onResume(MapActivity.java:111)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1354)
at android.app.Activity.performResume(Activity.java:7079)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3620)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3685)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2898)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
And here is my map activity where I call the movecamera
//onResume Method check which fragment intent is sent from
@Override
protected void onResume() {
super.onResume();
//make sure extras are not null
Bundle extras = getIntent().getExtras();
if (extras != null) {
this.receiveData();
Toast.makeText(this, "Received Data", Toast.LENGTH_SHORT).show();
}
}
private void receiveData() {
//RECEIVE DATA VIA INTENT
Intent i = getIntent();
if(i != null)
{
LatLng locationOne = i.getParcelableExtra("LOC_ONE");
LatLng locationTwo = i.getParcelableExtra("LOC_TWO");
Toast.makeText(this, "LocOne = " + locationOne + ", LocTwo = " + locationTwo, Toast.LENGTH_LONG).show();
//SET DATA TO TEXTVIEWS
locOne.setText(locationOne.toString());
locTwo.setText(locationTwo.toString());
// move camera to location one
Log.d(TAG, "receiveData: calling moveCamera");
//Map Crashes when trying to move camera
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(locationOne,15));
}
}
And my MoveCamera function
private void moveCamera(LatLng latLng, float zoom){
Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));
}
This is my first android app so any help is appreciated!
android google-maps-android-api-2
android google-maps-android-api-2
edited Nov 15 '18 at 19:02
Martin Zeitler
19k34373
19k34373
asked Nov 15 '18 at 18:54
user10405291user10405291
613
613
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
it obviously crashes, because mMap
is NULL
.
if(this.getIntent() != null && mMap != null) {
...
}
just had a closer look; you need to handle the intent onMapReady()
:
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
/* your code goes here */
} else {
/* regular initialization */
}
}
where onResume()
would possibly need to obtain the map from the SupportMapFragment
.
add a comment |
You're calling moveCamera inside the function itself besides why do you need your own function when there is a built in function to do so. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326177%2fapp-is-crashing-when-calling-movecamera-google-maps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
it obviously crashes, because mMap
is NULL
.
if(this.getIntent() != null && mMap != null) {
...
}
just had a closer look; you need to handle the intent onMapReady()
:
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
/* your code goes here */
} else {
/* regular initialization */
}
}
where onResume()
would possibly need to obtain the map from the SupportMapFragment
.
add a comment |
it obviously crashes, because mMap
is NULL
.
if(this.getIntent() != null && mMap != null) {
...
}
just had a closer look; you need to handle the intent onMapReady()
:
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
/* your code goes here */
} else {
/* regular initialization */
}
}
where onResume()
would possibly need to obtain the map from the SupportMapFragment
.
add a comment |
it obviously crashes, because mMap
is NULL
.
if(this.getIntent() != null && mMap != null) {
...
}
just had a closer look; you need to handle the intent onMapReady()
:
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
/* your code goes here */
} else {
/* regular initialization */
}
}
where onResume()
would possibly need to obtain the map from the SupportMapFragment
.
it obviously crashes, because mMap
is NULL
.
if(this.getIntent() != null && mMap != null) {
...
}
just had a closer look; you need to handle the intent onMapReady()
:
@Override
public void onMapReady(GoogleMap map) {
this.mMap = map;
if(this.getIntent() != null && this.getIntent().getAction() == INTENT_ACTION_LOCATION_SELECT) {
/* your code goes here */
} else {
/* regular initialization */
}
}
where onResume()
would possibly need to obtain the map from the SupportMapFragment
.
edited Nov 19 '18 at 16:55
answered Nov 15 '18 at 19:01
Martin ZeitlerMartin Zeitler
19k34373
19k34373
add a comment |
add a comment |
You're calling moveCamera inside the function itself besides why do you need your own function when there is a built in function to do so. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));
add a comment |
You're calling moveCamera inside the function itself besides why do you need your own function when there is a built in function to do so. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));
add a comment |
You're calling moveCamera inside the function itself besides why do you need your own function when there is a built in function to do so. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));
You're calling moveCamera inside the function itself besides why do you need your own function when there is a built in function to do so. mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(place.getLatLng(), 15.0f));
answered Nov 19 '18 at 17:53
Mavee ShahMavee Shah
226
226
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53326177%2fapp-is-crashing-when-calling-movecamera-google-maps%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown