2017-11-21 6 views
-2

umgewandelt werden. Bitte finden Sie den Code in meiner Hauptaktivität.
java.lang.ClassCastException: com.example.android.hereiam.MapsActivity kann nicht in com.google.android.gms.location.LocationListener

package com.example.android.hereiam; 

import android.support.annotation.RequiresApi; 
import android.support.v4.app.ActivityCompat; 
import android.os.Build; 
import android.os.Bundle; 

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

import android.support.v4.content.ContextCompat; 
import android.support.v4.app.FragmentActivity; 

import com.google.android.gms.gcm.GcmListenerService; 
import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.OnMapReadyCallback; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.SupportMapFragment; 

import android.content.pm.PackageManager; 
import android.location.Location; 

import com.google.android.gms.location.LocationListener; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 

// Since it’s the easiest way of adding a map to your project, I’m going to stick with using 




// a MapFragment// 

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, 



     GoogleApiClient.ConnectionCallbacks { 
    private GoogleMap mMap; 




    GoogleApiClient mGoogleApiClient; 




    Marker mLocationMarker; 
    Location mLastLocation; 




    LocationRequest mLocationRequest; 


    @RequiresApi(api = Build.VERSION_CODES.M) 




    @Override 




    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
      checkLocationPermission(); 
     } 


     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 


    public static final int MY_PERMISSIONS_REQUEST_LOCATION = 1; 

    @RequiresApi(api = Build.VERSION_CODES.M) 




    public boolean checkLocationPermission() { 



// In Android 6.0 and higher you need to request permissions at runtime, and the user has 
     // the ability to grant or deny each permission. Users can also revoke a previously-granted 
     // permission at any time, so your app must always check that it has access to each 
     // permission, before trying to perform actions that require that permission. Here, we’re using 
     // ContextCompat.checkSelfPermission to check whether this app currently has the 
     // ACCESS_COARSE_LOCATION permission 

     if (ContextCompat.checkSelfPermission(this, 
       android.Manifest.permission.ACCESS_COARSE_LOCATION) 




       // If your app does have access to COARSE_LOCATION, then this method will return 
       // PackageManager.PERMISSION_GRANTED// 




       != PackageManager.PERMISSION_GRANTED) { 
      // If your app doesn’t have this permission, then you’ll need to request it by calling 
// the ActivityCompat.requestPermissions method// 
      if (ActivityCompat.shouldShowRequestPermissionRationale(this, 
        android.Manifest.permission.ACCESS_COARSE_LOCATION)) 
       requestPermissions(new String[]{ 
           android.Manifest.permission.ACCESS_COARSE_LOCATION 
         }, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      else { 
       // Request the permission by launching Android’s standard permissions dialog. 
       // If you want to provide any additional information, such as why your app requires this 
       // particular permission, then you’ll need to add this information before calling 
       // requestPermission // 
       requestPermissions(new String[]{ 
           android.Manifest.permission.ACCESS_COARSE_LOCATION 
         }, 
         MY_PERMISSIONS_REQUEST_LOCATION); 
      } 
      return false; 
     } else { 
      return true; 
     } 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 
     // Specify what kind of map you want to display. In this example I’m sticking with the 
     // classic, “Normal” map 

     mMap.setMapType(GoogleMap.MAP_TYPE_NORMAL); 


     if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) ; 
     { 
      if (ContextCompat.checkSelfPermission(this, 
        android.Manifest.permission.ACCESS_COARSE_LOCATION) 
        == PackageManager.PERMISSION_GRANTED) { 
       buildGoogleApiClient(); 
       // Although the user’s location will update automatically on a regular basis, you can also 
       // give your users a way of triggering a location update manually. Here, we’re adding a 
       // ‘My Location’ button to the upper-right corner of our app; when the user taps this button, 
       // the camera will update and center on the user’s current location// 

       mMap.setMyLocationEnabled(true); 
      } else { 
       buildGoogleApiClient(); 
       mMap.setMyLocationEnabled(true); 
      } 
     } 
    } 

    protected synchronized void buildGoogleApiClient() { 
     // Use the GoogleApiClient.Builder class to create an instance of the 
     // Google Play Services API client// 
     mGoogleApiClient = new GoogleApiClient.Builder(this) 



       .addConnectionCallbacks(this) 
       .addApi(LocationServices.API) 
       .build(); 

     // Connect to Google Play Services, by calling the connect() method// 
     mGoogleApiClient.connect(); 
    } 

    @Override 
    // If the connect request is completed successfully, the onConnected(Bundle) method 
    // will be invoked and any queued items will be executed// 
    public void onConnected(Bundle bundle) { 




     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(2000); 
     if (ContextCompat.checkSelfPermission(this, 
       android.Manifest.permission.ACCESS_COARSE_LOCATION) 
       == PackageManager.PERMISSION_GRANTED) { 
      // Retrieve the user’s last known location// 
      LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, (LocationListener) this); 
     } 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
    } 

    // Displaying multiple ‘current location’ markers is only going to confuse your users! 
    // To make sure there’s only ever one marker onscreen at a time, I’m using 
    // mLocationMarker.remove to clear all markers whenever the user’s location changes. 
    public class MyGcmListenerService extends GcmListenerService { 

     @Override 
     public void onMessageReceived(String from, Bundle data) { 
      String message = data.getString("message"); 
     } 

     public void onLocationChanged(Location location) { 
      mLastLocation = location; 
      if (mLocationMarker != null) { 
       mLocationMarker.remove(); 
      } 

      // To help preserve the device’s battery life, you’ll typically want to use 
      // removeLocationUpdates to suspend location updates when your app is no longer 
      // visible onscreen// 
      if (mGoogleApiClient != null) { 
       LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this); 
      } 
     } 

     // Once the user has granted or denied your permission request, the Activity’s 
     // onRequestPermissionsResult method will be called, and the system will pass 
     // the results of the ‘grant permission’ dialog, as an int// 


     public void onRequestPermissionsResult(int requestCode, 
               String permissions[], int[] grantResults) { 
      switch (requestCode) { 
       case MY_PERMISSIONS_REQUEST_LOC`enter code here`ATION: { 

        // If the request is cancelled, the result array will be empty (0)// 
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

         // If the user has granted your permission request, then your app can now perform all its 
         // location-related tasks, including displaying the user’s location on the map// 
         if (ContextCompat.checkSelfPermission(this, 
           android.Manifest.permission.ACCESS_COARSE_LOCATION) 
           == PackageManager.PERMISSION_GRANTED) { 
          if (mGoogleApiClient == null) { 
           buildGoogleApiClient(); 
          } 
          mMap.setMyLocationEnabled(true); 
         } 
        } else { 
         // If the user has denied your permission request, then at this point you may want to 
         // disable any functionality that depends on this permission// 
        } 
        return; 
       } 
      } 
     } 
    } 
} 

Die Haupttätigkeit Code behandelt die Google api mir die richtigen Ort Orte zu geben.
Aber ich bin immer die Fehler:

Prozess: com.example.android.hereiam, PID: 15899 java.lang.ClassCastException: com.example.android.hereiam.MapsActivity nicht com gegossen werden kann. google.android.gms.location.LocationListener unter com.example.android.hereiam.MapsActivity.onConnected (MapsActivity.java:151) bei com.google.android.gms.common.internal.zzk.zzp (Unbekannte Quelle) bei com.google.android.gms.internal.zzrd.zzn (Unbekannte Quelle) bei com.google.android.gms.internal.zzrb.zzass (U nknown Quelle) bei com.google.android.gms.internal.zzrb.onConnected (Unbekannte Quelle) bei com.google.android.gms.internal.zzrf.onConnected (Unbekannte Quelle) bei com.google.android.gms .internal.zzqr.onConnected (Unbekannte Quelle) bei com.google.android.gms.common.internal.zzj $ 1.onConnected (Unbekannt Quelle) at com.google.android.gms.common.internal.zze $ zzj .zzavj (Unbekannt Quelle) bei com.google.android.gms.common.internal.zze $ zza.zzc (Unbekannte Quelle) bei com.google.android.gms.common.internal.zze $ zza.zzv (Unbekannte Quelle) bei com.google .android.gms.common.internal.zze $ zze.zzavl (Unbekannt Quelle) bei com.google.android.gms.common.internal.zze $ zzd.handleMessage (Unbekannt Quelle) bei android.os. Handler.dispatchMessage (Handler.java:102) bei android.os.Looper.loop (Looper.java:154) bei android.app.ActivityThread.main (ActivityThread.java:6692) bei java.lang.reflect. Method.invoke (systemeigene Methode) um com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1468) bei com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1358) 11-21 14: 45: 42.935 15899-15944/com.beispiel.android.hereiam I/System.out: (HTTPLog) -Static: isSBSettingEnabled false 11-21 14: 45: 42.935 15899-15944/com.beispiel.android .hereiam I/System.out: (HTTPLog) -Static: isSBsettingEnabled false 11-21 14: 45: 44.702 15899-15971/com.beispiel.android.hereiam W/DynamiteModule: Lokales Modul Deskriptorklasse für com.google .android.gms.googlecertificates nicht gefunden. 11-21 14: 45: 44.706 15899-15971/com.beispiel.android.hereiam W/DynamiteModule: Laden des Moduls über V2 fehlgeschlagen: java.lang.ClassNotFoundException: Die Klasse "com.google.android" wurde nicht gefunden .gms.dynamite.DynamiteModule $ DynamiteLoaderClassLoader " auf Pfad: DexPathList [[zip-Datei " /data/app/com.example.android.hereiam-1/base.apk ", zip-Datei "/data/app/com .beispiel.android.hereiam-1/split_lib_dependencies_apk.apk ", zip-Datei " /data/app/com.example.android.hereiam-1/split_lib_slice_0_apk.apk " Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_1_apk.apk", Zip-Datei " /data/app/com.example.android.hereiam-1/split_lib_slice_2_apk. apk " Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_3_apk.apk", Zip-Datei " /data/app/com.example.android.hereiam-1/split_lib_slice_4_apk. apk " Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_5_apk.apk", Zip-Datei " /data/app/com.example.android.hereiam-1/split_lib_slice_6_apk. apk“, Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_7_apk.apk", Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_8_apk.apk", Zip-Datei "/data/app/com.example.android.hereiam-1/split_lib_slice_9_apk.apk"] , nativeLibraryDirectories = [/ data/app/com.example.android.hereiam-1/lib/arm64, /System/lib64,/Verkäufer/lib64]] 11-21 14: 45: 44,737 15.899-15.971/com. example.android.hereiam I/DynamiteModule: Berücksichtigt lokales Modul com.google.android.gms.googlecertificates: 0 und remote Modul com.google.android.gms.googlezertifikate: 4 11-21 14: 45: 44.737 15899- 15971/com.example.android.hereiam I/DynamiteModule: Ausgewählte Remote-Version von com.google.android.gms.googlecertificates, Version

= 4 11-21 14: 45: 15.899 bis 15.971 44,763/com.example.android.hereiam W/System: Klassenladeprogramm unbekannt Pfad referenziert: /data/user_de/0/com.google.android.gms/app_chimera/m/00000038/n/arm64-V8A

Antwort

3

Sie sollten Location implementieren und versuchen Sie nicht, Ihre Aktivität zu Location zu werfen.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, LocationListener { 

Implementieren Sie die Location Methoden

und dies kann zu diesem

this 
0

Sie implementieren müssen Location in Ihnen MapsActivity Klasse

(LocationListener) this 

ersetzen. Sie können dann tun, wie fließt:

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

und diese Linie:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, (LocationListener) this); 

mit diesem:

LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this); 
+0

ich habe

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

damit diese Zeile ersetzen hinzugefügt wie angewiesen, aber immer noch bekommen Fehler. bitte was mache ich hier – Jonatino

Verwandte Themen