位置服务。 API:无法解析API

时间:2015-09-14 09:43:28

标签: android api google-maps

以下是MainActivity.Java文件。我已经导入了LocationService所需的所有软件包,但我仍然收到错误

"Cannot find Symbol Variable API"

以下是我收到错误的行。

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LOCATION_SERVICE.API)  //Cannot resolve symbol Variable
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

与版本一起安装的SDK工具列表。

  
      
  • Android SDK构建工具
  •   
  • Android SDK工具24.3.4
  •   
  • Android支持存储库,第19版
  •   
  • Android支持库,rev 23.0.1
  •   
  • Google Play服务,第26版
  •   
  • Google Repository,rev 21
  •   
  • Google Play APK扩展程序库,rev3
  •   
  • Android Auto API模拟器
  •   

代码:

package com.example.android.location2;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.location.Location;
import android.widget.TextView;
import android.util.Log;

import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.common.ConnectionResult;


import com.google.android.gms.common.api.GoogleApiClient;

public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener, LocationListener  {

    private final String LOG_TAG ="LaurenceTestApp";
    private TextView txtOutput;
    private GoogleApiClient mGoogleApiClient;
    private LocationRequest mLocationRequest;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Create a GoogleApiClient instance
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LOCATION_SERVICE.API)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();
        txtOutput= (TextView) findViewById(R.id.txtOutput);

    }



    @Override
    protected void onStart() {
        super.onStart();
        // Connect the client.
        mGoogleApiClient.connect();
    }

    @Override
    protected void onStop() {
        // Disconnecting the client invalidates it.
        mGoogleApiClient.disconnect();
        super.onStop();
    }

    @Override
    public void onConnected(Bundle bundle) {

        mLocationRequest = LocationRequest.create();
        mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
        mLocationRequest.setInterval(10); // Update location every second

        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

    }

    @Override
    public void onConnectionSuspended(int i) {
        Log.i(LOG_TAG, "GoogleApiClient connection has been suspend");
    }

    @Override
    public void onConnectionFailed(ConnectionResult connectionResult) {
        Log.i(LOG_TAG, "GoogleApiClient connection has failed");
    }

    @Override
    public void onLocationChanged(Location location) {
        Log.i(LOG_TAG, location.toString());
        //txtOutput.setText(location.toString());

        txtOutput.setText(Double.toString(location.getLatitude()));
    }


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

2 个答案:

答案 0 :(得分:1)

mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LOCATION_SERVICE.API)  //Cannot resolve symbol Variable
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

而不是Location_Service,它应该是LocationServices。改变它解决了这个问题。

感谢大家的帮助

答案 1 :(得分:0)

您似乎忘记在应用项目中导入 Google Play服务。这就是为什么您的应用代码和导入的包名称无法找到 gms 包的原因。使用Eclipse时,Android Studio和ADT Bundle的过程将有所不同。请参阅Setting up Google Play Services的官方文档。

如果您在Android Studio上进行开发,则需要在应用项目的build.gradle文件中添加依赖项并同步gradle文件。您可以参考以下SO post并查看答案。

如果您使用ADT Bundle在Eclipse上进行开发,那么情况会略有不同。将.jar文件复制和粘贴到您的应用程序目录中并将其导入您的开发环境中有一些手动工作。这样就可以识别 gms 包。请参考此步骤document并使用图表来完成此任务。

希望这有助于!!