Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法解析方法getlatitude()_Java_Android_Google Play Services - Fatal编程技术网

Java 无法解析方法getlatitude()

Java 无法解析方法getlatitude(),java,android,google-play-services,Java,Android,Google Play Services,我是编程新手,正在学习Google Location API教程。我已经正确地完成了所有步骤,但是,下面的代码在getlatitude()和getlatitude()处给出了错误。错误:无法解析方法getlatitude()。如果有人能指出这个问题,那就太好了。还有一件事需要注意,代码似乎从未使用过android.location.location类,我希望getlatitude()会从中产生 import android.app.Activity; import android.os.Bund

我是编程新手,正在学习Google Location API教程。我已经正确地完成了所有步骤,但是,下面的代码在getlatitude()和getlatitude()处给出了错误。错误:无法解析方法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
,初始化时不使用
字符串。valueOf
调用:

protected Location mLastLocation;
...
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);

非常感谢;我现在明白这应该是怎么回事了。我有滥用此线程的风险;代码可以工作,但我仍然无法显示我的纬度和经度坐标。我怀疑未在连接的部分中更新mLatitude和mLongitate。有没有办法检查连接是否真的建立了?@Rehan:恐怕不知道-我不是Android开发人员。但我怀疑如果你在Stack Overflow上搜索现有的问题,你会发现很多相关的问题。我会查看现有的帖子。再次感谢您帮助解决位置问题:)