您好,所以今天我要在我的Android应用程序中实现位置,但遇到了一个我无法理解的异常问题。
问题
我首先查看Googles官方文档以获取最后一个已知位置并实施但未成功。然后,我查看了提供的示例应用程序,并将确切的代码复制到我自己的应用程序中,但这也失败了。该应用程序永远不会产生任何错误,并且完全按照预期工作,但我只是没有获得位置。
在下面的代码中 mLastLocation 在我正在创建的应用程序中始终为null,但在google制作中完美无缺。请注意,我和谷歌都使用了这个确切的代码。
MainActivity
import android.location.Location;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;
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;
public class MainActivity extends AppCompatActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
protected static final String TAG = "MainActivity";
/**
* Provides the entry point to Google Play services.
*/
protected GoogleApiClient mGoogleApiClient;
/**
* Represents a geographical location.
*/
protected Location mLastLocation;
protected String mLatitudeLabel;
protected String mLongitudeLabel;
protected TextView mLatitudeText;
protected TextView mLongitudeText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
mLatitudeLabel = getResources().getString(R.string.latitude_label);
mLongitudeLabel = getResources().getString(R.string.longitude_label);
mLatitudeText = (TextView) findViewById((R.id.latitude_text));
mLongitudeText = (TextView) findViewById((R.id.longitude_text));
buildGoogleApiClient();
}
/**
* Builds a GoogleApiClient. Uses the addApi() method to request the LocationServices API.
*/
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
/**
* Runs when a GoogleApiClient object successfully connects.
*/
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mLastLocation != null) {
mLatitudeText.setText(String.format("%s: %f", mLatitudeLabel,
mLastLocation.getLatitude()));
mLongitudeText.setText(String.format("%s: %f", mLongitudeLabel,
mLastLocation.getLongitude()));
} else {
Toast.makeText(this, R.string.no_location_detected, Toast.LENGTH_LONG).show();
}
}
@Override
public void onConnectionFailed(ConnectionResult result) {
// Refer to the javadoc for ConnectionResult to see what error codes might be returned in
// onConnectionFailed.
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
@Override
public void onConnectionSuspended(int cause) {
// The connection to Google Play services was lost for some reason. We call connect() to
// attempt to re-establish the connection.
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
}
到目前为止我做了什么
经过几个小时的挫折后,我从零开始创建了一个全新的项目,并尝试尽可能复制官方样本,但仍然无效。所以现在我尝试在下载之后直接从Google运行示例代码,当然这有点像魅力,但我不明白为什么。 我还花了最后2个小时试图找出为什么git repo应用程序工作以及为什么我自己的重复它甚至不是所有的代码完全相同。
我需要帮助
代码本身似乎没有错误,因为代码在示例repo应用程序中工作并且它不会产生任何错误。我需要有人告诉我我错过了什么!
为了帮助您设置我的两个Android Studio项目的github仓库。不起作用的项目称为 BasicLocationSampleOwn ,有效的项目称为 BasicLocationSample 。
您可以在此处下载回购:https://github.com/CarlOhlsson/getLastLocationProblem
答案 0 :(得分:1)
通过从API 23降级到22来解决问题。在权限失败时,Android 6.0显然不会产生任何错误消息。我需要在清单文件中的权限之上请求用户的许可。
发现了Android 6.0权限信息here