2016-08-29 4 views
3

Ich bin mit einer Ausnahme in meiner Anwendung konfrontiert, die Ausnahme bezieht sich auf com.google.android.gms.location.LocationListner in android , bitte hilf mir in dieserkann nicht in com.google.android.gms.location.LocationListener Ausnahme in Android

Meine MainActivity.java Datei in Bezug auf:

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

Meine logcat Datei:

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) 

Meine Manifest.xml Datei:

<?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> 

Antwort

11

Wenn Sie einen Blick auf die logcat nehmen, erklärte der Fehler, dass Sie falsch Location importiert haben. Überprüfen Sie Ihre importierte Bibliothek im oberen Teil der Klasse, es sollte com.google.android.gms.location.LocationListener nicht android.location.LocationListener sein.

Verwandte Themen