- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我是编程新手,正在学习 Google Location API 教程。我已正确执行所有步骤,但是,以下代码在 getlatitude() 和 getlongitude() 处给我错误。错误:无法解析方法 getlatitude()。如果有人能指出这个问题,那就太好了。还有一件事要注意,代码似乎从不使用 android.location.Location 类,我希望 getlatitude() 会来自那里。
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
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 android.location.Location;
public class MainActivity extends Activity implements ConnectionCallbacks, OnConnectionFailedListener {
protected GoogleApiClient mGoogleApiClient;
/**
* Represents a geographical location.
*/
protected String mLastLocation;
protected static final String TAG = "basic-location-sample";
protected TextView mLatitudeText;
protected TextView mLongitudeText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLatitudeText = (TextView) findViewById((R.id.latitude_text));
mLongitudeText = (TextView) findViewById((R.id.longitude_text));
buildGoogleApiClient();
}
protected synchronized void buildGoogleApiClient() {
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener((OnConnectionFailedListener) this)
.addApi(LocationServices.API)
.build();
}
@Override
protected void onStart() {
super.onStart();
mGoogleApiClient.connect();
}
@Override
protected void onStop() {
super.onStop();
if (mGoogleApiClient.isConnected()) {
mGoogleApiClient.disconnect();
}
}
@Override
public void onConnected(Bundle connectionHint) {
mLastLocation = String.valueOf(LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient));
if (mLastLocation!= null) {
mLatitudeText.setText(String.valueOf(mLastLocation.getLatitude()));
mLongitudeText.setText(String.valueOf(mLastLocation.getLongitude()));
}
}
@Override
public void onConnectionSuspended(int i) {
Log.i(TAG, "Connection suspended");
mGoogleApiClient.connect();
}
@Override
public void onConnectionFailed(ConnectionResult result) {
Log.i(TAG, "Connection failed: ConnectionResult.getErrorCode() = " + result.getErrorCode());
}
}
最佳答案
查看mLastLocation
的类型:
protected String mLastLocation;
String
没有 getLatitude
和 getLongitude
方法。您的变量应该是 Location
类型,并且在没有 String.valueOf
调用的情况下进行初始化:
protected Location mLastLocation;
...
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
关于java - 无法解析方法 getlatitude(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27799668/
Android 的 location.getLatitude() 方法是否提供大地纬度或地心纬度? 如果是大地测量,它是基于 WGS84 还是 GR80 椭球体? API 规范令人惊讶地缺乏,它只声明
我是编程新手,正在学习 Google Location API 教程。我已正确执行所有步骤,但是,以下代码在 getlatitude() 和 getlongitude() 处给我错误。错误:无法解析方
我正在编写跟踪 map 。在我想要分配的 getDeviceLocation 方法中: prevLat=mLastKnownLocation.getLatitude(); prevLng=mLastK
我收到以下运行时错误:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“double android.location.Location.getLatitu
我正在开发一个应用程序,只要您单击获取位置 (gpsBtn),GPS 就会启用,用户的位置将显示在 TextView 中。我收到 java.lang.NullPointerException 错误,应
我正在处理的 Android 应用程序有问题。该应用程序工作正常,直到我添加了两行代码来获取用户的纬度和经度。当我添加此代码时,我的应用程序无缘无故崩溃。这是带有 getLatitude 和 getL
这个问题在这里已经有了答案: Android getlastknownlocation returns null (1 个回答) 关闭 9 年前。 我在这一行收到一个空指针异常: double la
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 4 年前。 请您解释一下为什么 getLon
这个问题在这里已经有了答案: What is a NullPointerException, and how do I fix it? (12 个答案) 关闭 8 年前。 我正在尝试获取当前的纬度和
我真的遇到了这个错误,已经尝试了我能想到的一切来避免 getLatitude 方法上的 NPE... 有人可以检查我的代码并推荐解决方案吗? 只有当我在纬度和经度的某些坐标中进行硬编码时,它才有效,但
我有一种方法可以在 map 上显示设备的当前位置。我使用 FusedLocationProviderClient 因为它似乎是最准确的。 在 currentLocation.getLatitude()
如何将 getLongitude() 和 getLatitude() 的值转换为 EditText 可以接受的格式? 如果我像这样把它们放到 Toast 中就可以了 String message =
状态:大约一周后接受最佳可用答案并表示感谢。等待/邀请更多答案并引用 这件事很复杂。我真的很感谢所有评论员,特别是 asciimo、Gabe Sechan、AlexWien。请在适当研究后进一步投票和
为什么我不知道,我犯了这个错误。程序之前运行过。但现在我犯了这个错误。谢谢你的帮助... MenuActivity.java: final Context context = this; privat
所以我整天都在为此工作,我快要失去理智了。我正在尝试从谷歌的位置 api 获取坐标,以便从另一个 api (forcast.io) 获取天气。我的代码已经达到这样的程度,即 google api 正在
我一直在寻找答案,但也许我只是没有使用正确的术语。我正在创建一个应用程序,它将访问数据库以返回距用户位置一定距离内的其他用户的列表。我从未使用过此类数据,而且我真的不知道这些值的含义。我想使用 MyS
我正在尝试使用 openstreetmap osmosis 读取机场的 pbf 文件并提取登机口和跑道等特征。 我使用的代码类似于:http://www.javaoptimum.com/how-to-
我想要做的就是让用户获得他喜欢的类型的位置列表。例如,如果输入是医院,我的应用程序将使用搜索字符串“医院”打开谷歌地图。但正如文档中所建议的那样,使用像 geo:0,0?q=hospital 这样的地
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 5 年前。 我正在 android 中创建一
当我使用 Mapbox 时,我遇到了与 GPS 相关的问题。 public class MainActivity extends AppCompatActivity implements OnMapR
我是一名优秀的程序员,十分优秀!