这是我的活动,我想进入它的位置。我已将活动和地理位置功能分离了
package com.salvo.weather.android.activity;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import com.google.android.gms.common.api.GoogleApiClient;
import com.salvo.weather.android.R;
import com.salvo.weather.android.geolocation.CurrentGeolocation;
public class TodayActivity extends Activity {
public static final String TAG = TodayActivity.class.getSimpleName();
protected GoogleApiClient mGoogleApiClient;
protected CurrentGeolocation mCurrentGeolocation;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_today);
mCurrentGeolocation = new CurrentGeolocation(this);
mGoogleApiClient = mCurrentGeolocation.getmGoogleApiClient();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
Log.i(TAG, String.valueOf(mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation().getLatitude()));
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_today, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是地理位置功能:
package com.salvo.weather.android.geolocation;
import android.content.Context;
import android.location.Location;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.GoogleApiClient.ConnectionCallbacks;
import com.google.android.gms.common.api.GoogleApiClient.OnConnectionFailedListener;
import com.google.android.gms.location.LocationServices;
import com.salvo.weather.android.entity.CurrentGeolocationEntity;
/**
* Created by mazzy on 30/05/15.
*/
public class CurrentGeolocation
implements ConnectionCallbacks, OnConnectionFailedListener {
private Context mcontext;
private GoogleApiClient mGoogleApiClient;
private CurrentGeolocationEntity mCurrentGeolocationEntity;
public CurrentGeolocation(Context context) {
mcontext = context;
mCurrentGeolocationEntity = new CurrentGeolocationEntity();
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
this.mGoogleApiClient = new GoogleApiClient.Builder(this.mcontext)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
public void onConnected(Bundle bundle) {
Location mLastLocation = LocationServices
.FusedLocationApi
.getLastLocation(this.mGoogleApiClient);
if (mLastLocation != null) {
mCurrentGeolocationEntity.setmLastLocation(mLastLocation);
} else {
Toast.makeText(this.mcontext, "No location detected", Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionSuspended(int i) {
// The connection to Google Play services was lost for some reason. We call connect() to
// attempt to re-establish the connection.
// TODO: add some verbose message
}
@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
}
public GoogleApiClient getmGoogleApiClient() {
return mGoogleApiClient;
}
public CurrentGeolocationEntity getmCurrentGeolocationEntity() {
return mCurrentGeolocationEntity;
}
}
班级CurrentGeolocationEntity
是一个简单的POJO,我存储了代表已知最后位置的位置。
不明白我得到错误:
Attempt to invoke virtual method 'double android.location.Location.getLatitude()' on a null object reference
我不知道如何弄明白。你有什么建议吗?
答案 0 :(得分:1)
看起来您有竞争条件,并且在SDK有时间连接之前,您需要从CurrentGeolocation
课程中请求某个位置。
您可以向isConnected()
类添加CurrentGeolocation
方法,并在onConnected()
回调中设置布尔值。
然后,在致电isConnected()
之前,请始终致电mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation()
。
另外需要提及的是getLastLocation()
返回null的概率很高,因为它没有明确地发出位置请求。您从此方法获得的位置也可能非常旧,可能根本不会反映当前位置。
你应该做的第一个修复,在使用它之前对Location对象进行空检查:
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
//use a temp Location object:
Location loc = mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation();
//null check:
if (loc != null){
Log.i(TAG, String.valueOf(loc.getLatitude()));
}
}
您应该做的下一个更改是在CurrentGeolocation
类中注册位置监听器,该监听器将明确地从系统请求新位置。
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10);
mLocationRequest.setFastestInterval(10);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
mLocationRequest.setSmallestDisplacement(0.1F);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);
然后添加onLocationChanged()
回调:
@Override
public void onLocationChanged(Location mLastLocation) {
mCurrentGeolocationEntity.setmLastLocation(mLastLocation);
}
有关详细信息,请查看this answer中的代码。
请注意,您应该尽快取消注册位置监听器,以尽量减少应用的耗电量:
LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);
答案 1 :(得分:0)
您的问题出现在您的活动onStart()
中,在此行:
Log.i(TAG, String.valueOf(mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation().getLatitude()));
问题是以下一行:
mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation()
返回null。我现在只能猜测,但我认为当你的活动首次启动时你没有最后的位置,所以该方法返回null。请尝试以下代码:
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
if (mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation() != null)
Log.i(TAG, String.valueOf(mCurrentGeolocation.getmCurrentGeolocationEntity().getmLastLocation().getLatitude()));
}