【问题标题】:Location_Services. API: Not Able to resolve API位置服务。 API:无法解析 API
【发布时间】:2015-09-14 09:43:27
【问题描述】:

下面是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 支持库,修订版 23.0.1
  • Google Play 服务,第 26 版
  • Google 代码库,第 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);
    }
}

【问题讨论】:

  • 所以我假设你已经导入了那个包?
  • 您好 Eenvincible,我已经导入了代码中存在的包。据我所知,那是 require 包。

标签: android api google-maps


【解决方案1】:
mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addApi(LOCATION_SERVICE.API)  //Cannot resolve symbol Variable
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .build();

应该是 LocationServices,而不是 Location_Service。更改它可以解决问题。

感谢大家的帮助

【讨论】:

    【解决方案2】:

    您似乎忘记在您的应用项目中导入 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 与图表来完成此任务。

    希望这有帮助!

    【讨论】:

    • 感谢您的回复。我已经尝试使用此链接“[link] developers.google.com/android/guides/setup”来设置 Google Play 服务。我已经包含了“依赖项 { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.android.support:appcompat-v7:23.0.1' compile 'com.google.android.gms :play-services:7.8.0' }" 在依赖项中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-05-02
    • 2021-01-21
    • 2020-01-06
    • 2017-07-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多