UncaughtException ExceptionInInitializerError

时间:2019-01-28 13:25:45

标签: android

在尝试确定设备的当前位置时,我收到E / UncaughtException:java.lang.ExceptionInInitializerError错误。日志猫似乎正在识别“ mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);”作为一个错误,但我看不出为什么。 尝试确定设备的当前位置时,我收到E / UncaughtException:java.lang.ExceptionInInitializerError错误。日志猫似乎正在识别“ mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);”作为一个错误,但我看不出为什么。

import android.*;
import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.WindowManager;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.location.FusedLocationProviderClient;
import com.google.android.gms.location.LocationServices;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by User on 10/2/2017.
 */

public class CustomerHome extends AppCompatActivity implements OnMapReadyCallback {
//First the devices location is found using getLocationPermission();
// If permissions are granted the map is initialised using the  initMap();
//After the map is initliazed the device's location is requested using the getDeviceLocation();  method

    @Override
    public void onMapReady(GoogleMap googleMap) {
        Toast.makeText(this, "Map is Ready", Toast.LENGTH_SHORT).show();
        Log.d(TAG, "onMapReady: map is ready");
        mMap = googleMap;

        if (mLocationPermissionsGranted) {
           getDeviceLocation();

           if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this,
                   Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            mMap.setMyLocationEnabled(true);
            mMap.getUiSettings().setMyLocationButtonEnabled(false);

          // init();
        }
    }

    private static final String TAG = "MapActivity";

    private static final String FINE_LOCATION = Manifest.permission.ACCESS_FINE_LOCATION;
    private static final String COURSE_LOCATION = Manifest.permission.ACCESS_COARSE_LOCATION;
    private static final int LOCATION_PERMISSION_REQUEST_CODE = 1234;
    private static final float DEFAULT_ZOOM = 15f;

    //widgets
    private EditText mSearchText;
    private ImageView mGps;

    //vars
    private Boolean mLocationPermissionsGranted = false;
    private GoogleMap mMap;
    private FusedLocationProviderClient mFusedLocationProviderClient;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_customer_home);
       // mSearchText = (EditText) findViewById(R.id.input_search);
        mGps = (ImageView) findViewById(R.id.ic_gps);

        getLocationPermission();

    }

   /* private void init(){
        Log.d(TAG, "init: initializing");

        mSearchText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int actionId, KeyEvent keyEvent) {
                if(actionId == EditorInfo.IME_ACTION_SEARCH
                        || actionId == EditorInfo.IME_ACTION_DONE
                        || keyEvent.getAction() == KeyEvent.ACTION_DOWN
                        || keyEvent.getAction() == KeyEvent.KEYCODE_ENTER){

                    //execute our method for searching
                    geoLocate();
                }

                return false;
            }
        });

        mGps.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Log.d(TAG, "onClick: clicked gps icon");
                getDeviceLocation();
            }
        });

     //   hideSoftKeyboard();
    }*/

    private void geoLocate(){
        Log.d(TAG, "geoLocate: geolocating");

        String searchString = mSearchText.getText().toString();

        Geocoder geocoder = new Geocoder(CustomerHome.this);
        List<Address> list = new ArrayList<>();
        try{
            list = geocoder.getFromLocationName(searchString, 1);
        }catch (IOException e){
            Log.e(TAG, "geoLocate: IOException: " + e.getMessage() );
        }

        if(list.size() > 0){
            Address address = list.get(0);

            Log.d(TAG, "geoLocate: found a location: " + address.toString());
            //Toast.makeText(this, address.toString(), Toast.LENGTH_SHORT).show();

            moveCamera(new LatLng(address.getLatitude(), address.getLongitude()), DEFAULT_ZOOM,
                    address.getAddressLine(0));
        }
    }

    private void getDeviceLocation(){
        Log.d(TAG, "getDeviceLocation: getting the devices current location");

        mFusedLocationProviderClient = LocationServices.getFusedLocationProviderClient(this);

        try{
            if(mLocationPermissionsGranted){

                final Task location = mFusedLocationProviderClient.getLastLocation();
                location.addOnCompleteListener(new OnCompleteListener() {
                    @Override
                    public void onComplete(@NonNull Task task) {
                        if(task.isSuccessful()){
                            Log.d(TAG, "onComplete: found location!");
                            Location currentLocation = (Location) task.getResult();

                            moveCamera(new LatLng(currentLocation.getLatitude(), currentLocation.getLongitude()),
                                    DEFAULT_ZOOM,
                                    "My Location");

                        }else{
                            Log.d(TAG, "onComplete: current location is null");
                            Toast.makeText(CustomerHome.this, "unable to get current location", Toast.LENGTH_SHORT).show();
                        }
                    }
                });
            }
        }catch (SecurityException e){
            Log.e(TAG, "getDeviceLocation: SecurityException: " + e.getMessage() );
        }
    }

    private void moveCamera(LatLng latLng, float zoom, String title){
        Log.d(TAG, "moveCamera: moving the camera to: lat: " + latLng.latitude + ", lng: " + latLng.longitude );
        mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, zoom));

        if(!title.equals("My Location")){
            MarkerOptions options = new MarkerOptions()
                    .position(latLng)
                    .title(title);
            mMap.addMarker(options);
        }

       // hideSoftKeyboard();
    }

    private void initMap(){
        Log.d(TAG, "initMap: initializing map");
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);

        mapFragment.getMapAsync(CustomerHome.this);
    }

    private void getLocationPermission(){
        Log.d(TAG, "getLocationPermission: getting location permissions");
        String[] permissions = {Manifest.permission.ACCESS_FINE_LOCATION,
                Manifest.permission.ACCESS_COARSE_LOCATION};

        if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
            if(ContextCompat.checkSelfPermission(this.getApplicationContext(),
                    COURSE_LOCATION) == PackageManager.PERMISSION_GRANTED){
                mLocationPermissionsGranted = true;
                initMap();
            }else{
                ActivityCompat.requestPermissions(this,
                        permissions,
                        LOCATION_PERMISSION_REQUEST_CODE);
            }
        }else{
            ActivityCompat.requestPermissions(this,
                    permissions,
                    LOCATION_PERMISSION_REQUEST_CODE);
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        Log.d(TAG, "onRequestPermissionsResult: called.");
        mLocationPermissionsGranted = false;

        switch(requestCode){
            case LOCATION_PERMISSION_REQUEST_CODE:{
                if(grantResults.length > 0){
                    for(int i = 0; i < grantResults.length; i++){
                        if(grantResults[i] != PackageManager.PERMISSION_GRANTED){
                            mLocationPermissionsGranted = false;
                            Log.d(TAG, "onRequestPermissionsResult: permission failed");
                            return;
                        }
                    }
                    Log.d(TAG, "onRequestPermissionsResult: permission granted");
                    mLocationPermissionsGranted = true;
                    //initialize our map
                    initMap();
                }
            }
        }
    }


Logcat error:
01-28 13:15:48.320 7783-7783/com.example.julie.fyp E/UncaughtException: java.lang.ExceptionInInitializerError
        at com.example.julie.fyp.CustomerHome.getDeviceLocation(CustomerHome.java:589)
        at com.example.julie.fyp.CustomerHome.onMapReady(CustomerHome.java:491)
        at com.google.android.gms.maps.zzak.zza(Unknown Source)
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:310)
        at fu.b(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):15)
        at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4)
        at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.common.api.Api$zzf
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source)
        at com.example.julie.fyp.CustomerHome.getDeviceLocation(CustomerHome.java:589) 
        at com.example.julie.fyp.CustomerHome.onMapReady(CustomerHome.java:491) 
        at com.google.android.gms.maps.zzak.zza(Unknown Source) 
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source) 
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source) 
        at android.os.Binder.transact(Binder.java:310) 
        at fu.b(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):15) 
        at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4) 
        at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:5365) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 
01-28 13:15:48.320 7783-7783/com.example.julie.fyp E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.ExceptionInInitializerError
        at com.example.julie.fyp.CustomerHome.getDeviceLocation(CustomerHome.java:589)
        at com.example.julie.fyp.CustomerHome.onMapReady(CustomerHome.java:491)
        at com.google.android.gms.maps.zzak.zza(Unknown Source)
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source)
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source)
        at android.os.Binder.transact(Binder.java:310)
        at fu.b(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):15)
        at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4)
        at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4)
        at android.os.Handler.handleCallback(Handler.java:725)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:176)
        at android.app.ActivityThread.main(ActivityThread.java:5365)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
        at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.common.api.Api$zzf
        at com.google.android.gms.location.LocationServices.<clinit>(Unknown Source)
        at com.example.julie.fyp.CustomerHome.getDeviceLocation(CustomerHome.java:589) 
        at com.example.julie.fyp.CustomerHome.onMapReady(CustomerHome.java:491) 
        at com.google.android.gms.maps.zzak.zza(Unknown Source) 
        at com.google.android.gms.maps.internal.zzaq.dispatchTransaction(Unknown Source) 
        at com.google.android.gms.internal.maps.zzb.onTransact(Unknown Source) 
        at android.os.Binder.transact(Binder.java:310) 
        at fu.b(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):15) 
        at com.google.android.gms.maps.internal.bg.a(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4) 
        at com.google.maps.api.android.lib6.impl.bj.run(:com.google.android.gms.dynamite_mapsdynamite@14799007@14.7.99 (000304-223214910):4) 
        at android.os.Handler.handleCallback(Handler.java:725) 
        at android.os.Handler.dispatchMessage(Handler.java:92) 
        at android.os.Looper.loop(Looper.java:176) 
        at android.app.ActivityThread.main(ActivityThread.java:5365) 
        at java.lang.reflect.Method.invokeNative(Native Method) 
        at java.lang.reflect.Method.invoke(Method.java:511) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869) 
        at dalvik.system.NativeStart.main(Native Method) 

1 个答案:

答案 0 :(得分:0)

确保您的build.gradle文件中具有以下依赖项

com.google.android.gms:play-services-gcm:$version com.google.android.gms:play-services-location:$version