java - 无法转换为 com.google.android.gms.location.LocationListener android 中的异常

标签 java android android-studio

我在我的应用程序中遇到异常,该异常与 android 中的 com.google.android.gms.location.LocationListner 有关,请在这方面帮助我

我的MainActivity.java 文件:

package com.ideabiz.fusedlocationprovider;

import android.content.pm.PackageManager;
import android.location.Location;
import android.location.LocationListener;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

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


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


TextView txtOutputLat, txtOutputLon;
Location mLastLocation;
private GoogleApiClient mGoogleApiClient;
private LocationRequest mLocationRequest;
String lat, lon;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    txtOutputLat = (TextView) findViewById(R.id.textView);
    txtOutputLon = (TextView) findViewById(R.id.textView2);


    buildGoogleApiClient();
}


@Override
public void onConnected(Bundle bundle) {


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

    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (com.google.android.gms.location.LocationListener) this);


    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
            mGoogleApiClient);
    if (mLastLocation != null) {
        lat = String.valueOf(mLastLocation.getLatitude());
        lon = String.valueOf(mLastLocation.getLongitude());

    }
    updateUI();
}

@Override
public void onConnectionSuspended(int i) {

}

@Override
public void onLocationChanged(Location location) {
    lat = String.valueOf(location.getLatitude());
    lon = String.valueOf(location.getLongitude());
    updateUI();
}

@Override
public void onStatusChanged(String s, int i, Bundle bundle) {

}

@Override
public void onProviderEnabled(String s) {

}

@Override
public void onProviderDisabled(String s) {

}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    buildGoogleApiClient();
}

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 onDestroy() {
    super.onDestroy();
    mGoogleApiClient.disconnect();
}

void updateUI() {
    txtOutputLat.setText(lat);
    txtOutputLon.setText(lon);
}
}

我的 logcat 文件:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.ideabiz.fusedlocationprovider, PID: 18114
java.lang.ClassCastException: com.ideabiz.fusedlocationprovider.MainActivity cannot be cast to com.google.android.gms.location.LocationListener
at com.ideabiz.fusedlocationprovider.MainActivity.onConnected(MainActivity.java:52)

我的 Manifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ideabiz.fusedlocationprovider">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version" />
</application>

</manifest>

最佳答案

如果您查看 logcat,错误表明您导入了错误的 LocationListener。检查类顶部导入的库,它应该是 com.google.android.gms.location.LocationListener 而不是 android.location.LocationListener

关于java - 无法转换为 com.google.android.gms.location.LocationListener android 中的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39199250/

相关文章:

java - 运行 jar 包应用程序时出现 Hibernate InvalidMappingException

java - 如何使用Gson解析这种json字符串?

android-studio - 什么是在 gradle 文件中声明的 minifyEnabled。请问下面block的代码的含义和用法

android - 操作栏 setDisplayHomeAsUpEnabled 关闭应用程序

android - 执行数据绑定(bind)后更改包名称

java - 我的 ListView 未显示(Android Studio)

Java如何从字节流中读取64位unsigned long

Java 包不公开,包外无法访问

java - 使用正则表达式组从字符串中提取值

android - 如何使一组 View 在android中不可见