0
votes

I have to put the actual value of my location in a variable but I have this error:

java.lang.IllegalStateException: Could not execute method of the activity

My code is:

@Override
public void onLocationChanged(Location location) {
    mCurrentLocation = location;
    Latit = mCurrentLocation.getLatitude();

    Location objLoc = new Location("");
    objLoc.setLatitude(location.getLatitude());
    objLoc.setLongitude(location.getLongitude());
}

@Override
public void onConnected(Bundle bundle) {
    startLocationUpdates();
}

public void locInicio(View v) {
    TextView inicioLat = (TextView) findViewById(R.id.eInicioLat);
    Latit = mCurrentLocation.getLatitude();
    inicioLat.setText((int) Latit);

}

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

In other activity I can get the value with onLocationChanged but it always returns an error.

04-16 23:01:43.447 12954-12954/greetrack.estg.ipvc.greentrack E/Zygote﹕ MountEmulatedStorage() 04-16 23:01:43.447 12954-12954/greetrack.estg.ipvc.greentrack E/Zygote﹕ v2 04-16 23:01:43.467 12954-12954/greetrack.estg.ipvc.greentrack E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL 04-16 23:01:55.647 12954-12954/greetrack.estg.ipvc.greentrack E/ViewRootImpl﹕ sendUserActionEvent() mView == null 04-16 23:01:56.447 12954-12954/greetrack.estg.ipvc.greentrack E/AndroidRuntime﹕ FATAL EXCEPTION: main Process: greetrack.estg.ipvc.greentrack, PID: 12954 java.lang.IllegalStateException: Could not execute method of the activity at android.view.View$1.onClick(View.java:4221) at android.view.View.performClick(View.java:5155) at android.view.View$PerformClick.run(View.java:20747) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:145) at android.app.ActivityThread.main(ActivityThread.java:5832) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at android.view.View$1.onClick(View.java:4216)             at android.view.View.performClick(View.java:5155)             at android.view.View$PerformClick.run(View.java:20747)             at android.os.Handler.handleCallback(Handler.java:739)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:145)             at android.app.ActivityThread.main(ActivityThread.java:5832)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194) Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at greetrack.estg.ipvc.greentrack.Adicionar.locInicio(Adicionar.java:53)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)             at android.view.View$1.onClick(View.java:4216)             at android.view.View.performClick(View.java:5155)             at android.view.View$PerformClick.run(View.java:20747)             at android.os.Handler.handleCallback(Handler.java:739)             at android.os.Handler.dispatchMessage(Handler.java:95)             at android.os.Looper.loop(Looper.java:145)             at android.app.ActivityThread.main(ActivityThread.java:5832)             at java.lang.reflect.Method.invoke(Native Method)             at java.lang.reflect.Method.invoke(Method.java:372)

1
Post the full stack trace.Gabe Sechan
ok, i look in my questionSaPires
You simply need to take care of cases where location is null in onLocationChanged. e.g. if (location == null) return; as the fist line in the method.cYrixmorten

1 Answers

0
votes

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference at greetrack.estg.ipvc.greentrack.Adicionar.locInicio(Adicionar.java:53)

You simply need to take care of cases where location is null in onLocationChanged. e.g. if (location == null) return; as the fist line in the method.

@Override
public void onLocationChanged(Location location) {
    if (location == null) return;

    mCurrentLocation = location;
    Latit = mCurrentLocation.getLatitude();

    // by the way - why do you do this??
    Location objLoc = new Location("");
    objLoc.setLatitude(location.getLatitude());
    objLoc.setLongitude(location.getLongitude());
}

public void locInicio(View v) {
    TextView inicioLat = (TextView) findViewById(R.id.eInicioLat);

    // here is your problem
    if (mCurrentLocation == null) return; 

    Latit = mCurrentLocation.getLatitude();
    inicioLat.setText((int) Latit);

}