在Android中的Activity上获取位置时出错

时间:2015-04-16 21:51:25

标签: android google-maps

我必须将我的位置的实际值放在变量中,但是我有这个错误:

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

我的代码是:

@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();
}

在其他活动中,我可以使用onLocationChanged获取值,但它始终会返回错误。

  

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:变量seinfo:默认灵敏度: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   例外:主要       处理:greetrack.estg.ipvc.greentrack,PID:12954       java.lang.IllegalStateException:无法执行活动的方法               在android.view.View $ 1.onClick(View.java:4221)               在android.view.View.performClick(View.java:5155)               在android.view.View $ PerformClick.run(View.java:20747)               在android.os.Handler.handleCallback(Handler.java:739)               在android.os.Handler.dispatchMessage(Handler.java:95)               在android.os.Looper.loop(Looper.java:145)               在android.app.ActivityThread.main(ActivityThread.java:5832)               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)        引起:java.lang.reflect.InvocationTargetException               at java.lang.reflect.Method.invoke(Native Method)               在java.lang.reflect.Method.invoke(Method.java:372)               在android.view.View $ 1.onClick(View.java:4216)   在android.view.View.performClick(View.java:5155)   在android.view.View $ PerformClick.run(View.java:20747)   在android.os.Handler.handleCallback(Handler.java:739)   在android.os.Handler.dispatchMessage(Handler.java:95)   在android.os.Looper.loop(Looper.java:145)   在android.app.ActivityThread.main(ActivityThread.java:5832)   at java.lang.reflect.Method.invoke(Native Method)   在java.lang.reflect.Method.invoke(Method.java:372)   在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1399)   在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)        引起:java.lang.NullPointerException:尝试调用虚拟方法'double android.location.Location.getLatitude()'on a   null对象引用               在greetrack.estg.ipvc.greentrack.Adicionar.locInicio(Adicionar.java:53)   at java.lang.reflect.Method.invoke(Native Method)   在java.lang.reflect.Method.invoke(Method.java:372)   在android.view.View $ 1.onClick(View.java:4216)   在android.view.View.performClick(View.java:5155)   在android.view.View $ PerformClick.run(View.java:20747)   在android.os.Handler.handleCallback(Handler.java:739)   在android.os.Handler.dispatchMessage(Handler.java:95)   在android.os.Looper.loop(Looper.java:145)   在android.app.ActivityThread.main(ActivityThread.java:5832)   at java.lang.reflect.Method.invoke(Native Method)   在java.lang.reflect.Method.invoke(Method.java:372)

1 个答案:

答案 0 :(得分:0)

  

java.lang.NullPointerException:尝试在greetrack.estg.ipvc.greentrack.Adicionar.locInicio(Adicionar.java:53)的空对象引用上调用虚方法'double android.location.Location.getLatitude()'

您只需要处理onLocationChanged中位置为空的情况。例如if (location == null) return;作为方法中的第一行。

@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);

}