Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot be cast to com.google.android.gms.location.LocationListener exception in android

i am facing an exception in my application, the exception is related to com.google.android.gms.location.LocationListner in android, please help me in this regards

My MainActivity.java file:

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);
}
}

My logcat file:

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)

My Manifest.xml file:

<?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>
like image 674
Ram Koti Avatar asked Aug 29 '16 05:08

Ram Koti


1 Answers

If you take a look on the logcat, the error stated that you have imported wrong LocationListener. Check on your imported library at the top part of the class, it supposed to be com.google.android.gms.location.LocationListener not android.location.LocationListener.

like image 79
Amad Yus Avatar answered Oct 05 '22 22:10

Amad Yus