构建我的应用程序并安装 Google Maps API 后,每当我倾向于搜索位置时,我的应用程序就会崩溃

Posted

技术标签:

【中文标题】构建我的应用程序并安装 Google Maps API 后,每当我倾向于搜索位置时,我的应用程序就会崩溃【英文标题】:After builing my app and Installing Google Maps API, My APP crashes anytime I tend to search for Location 【发布时间】:2019-02-26 17:19:40 【问题描述】:

我是这个编程的新手,我发现很难通过这个阶段。成功构建APP后,我添加了谷歌地图以获取实时位置。执行此操作后,每次单击切换按钮时,我的应用都会崩溃。

如果您能帮我找出问题的根源,我会很高兴,因为这对我有很大帮助。下面是我的Logcat

09-22 15:59:04.636 4803-4803/com.example.mac.uberclone E/androidRuntime: FATAL EXCEPTION: main
Process: com.example.mac.uberclone, PID: 4803
java.lang.NullPointerException: Attempt to read from field 'int com.google.android.gms.location.LocationRequest.a' on a null object reference
    at android.os.Parcel.createException(Parcel.java:1948)
    at android.os.Parcel.readException(Parcel.java:1910)
    at android.os.Parcel.readException(Parcel.java:1860)
    at com.google.android.gms.internal.location.zza.transactAndReadExceptionReturnVoid(Unknown Source:10)
    at com.google.android.gms.internal.location.zzap.zza(Unknown Source:9)
    at com.google.android.gms.internal.location.zzas.zza(Unknown Source:44)
    at com.google.android.gms.internal.location.zzaz.zza(Unknown Source:5)
    at com.google.android.gms.internal.location.zzr.doExecute(Unknown Source:25)
    at com.google.android.gms.common.api.internal.BaseImplementation$ApiMethodImpl.run(Unknown Source:10)
    at com.google.android.gms.common.api.internal.zzag.execute(Unknown Source:71)
    at com.google.android.gms.common.api.internal.zzbd.execute(Unknown Source:5)
    at com.google.android.gms.common.api.internal.zzav.execute(Unknown Source:137)
    at com.google.android.gms.internal.location.zzq.requestLocationUpdates(Unknown Source:14)
    at com.example.mac.uberclone.Welcome.startLocationUpdate(Welcome.java:269)
    at com.example.mac.uberclone.Welcome.onConnected(Welcome.java:283)
    at com.google.android.gms.common.internal.GmsClientEventManager.onConnectionSuccess(Unknown Source:109)
    at com.google.android.gms.common.api.internal.zzav.zzb(Unknown Source:22)
    at com.google.android.gms.common.api.internal.zzaj.zzat(Unknown Source:92)
    at com.google.android.gms.common.api.internal.zzaj.onConnected(Unknown Source:21)
    at com.google.android.gms.common.api.internal.zzbd.onConnected(Unknown Source:7)
    at com.google.android.gms.common.api.internal.zzp.onConnected(Unknown Source:5)
    at com.google.android.gms.common.internal.zzf.onConnected(Unknown Source:2)
    at com.google.android.gms.common.internal.BaseGmsClient$PostInitCallback.handleServiceSuccess(Unknown Source:130)
    at com.google.android.gms.common.internal.BaseGmsClient$zza.deliverCallback(Unknown Source:62)
    at com.google.android.gms.common.internal.BaseGmsClient$zza.deliverCallback(Unknown Source:2)
    at com.google.android.gms.common.internal.BaseGmsClient$CallbackProxy.deliverCallback(Unknown Source:51)
    at com.google.android.gms.common.internal.BaseGmsClient$zzb.handleMessage(Unknown Source:270)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6669)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

也在我的 Build Gradle 应用中

dependencies 

    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.github.glomadrian:MaterialAnimatedSwitch:1.1@aar'
    implementation 'com.firebase:geofire-android:2.3.1'
    implementation 'com.android.support:appcompat-v7:28.0.0-rc02'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'
    implementation 'com.google.firebase:firebase-auth:16.0.3'
    implementation 'com.google.android.gms:play-services-maps:15.0.1'
    implementation 'com.google.android.gms:play-services-location:15.0.1'
    implementation 'com.google.android.gms:play-services-analytics:16.0.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'

    //Add Library
    implementation 'com.github.d-max:spots-dialog:0.7@aar'
    implementation 'uk.co.chrisjenx:calligraphy:latest.integration'
    implementation 'com.android.support:appcompat-v7:28.0.0'
    implementation 'com.rengwuxian.materialedittext:library:2.1.4'
    implementation 'com.android.support:design:28.0.0'
    implementation 'com.android.support:cardview-v7:28.0.0'
    implementation 'com.google.firebase:firebase-core:16.0.3'
    implementation 'com.google.firebase:firebase-database:16.0.2'
    implementation 'com.firebaseui:firebase-ui-database:4.1.0'
    implementation 'com.android.support.constraint:constraint-layout:1.1.3'


apply plugin: 'com.google.gms.google-services'

这是我的欢迎活动

package com.example.mac.uberclone;

import android.Manifest;
import android.content.pm.PackageManager;
import android.location.Location;
import android.os.Handler;
import android.os.SystemClock;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.FragmentActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.animation.Interpolator;
import android.view.animation.LinearInterpolator;
import android.widget.Toast;

import com.firebase.geofire.GeoFire;
import com.firebase.geofire.GeoLocation;
import com.github.glomadrian.materialanimatedswitch.MaterialAnimatedSwitch;
import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
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.BitmapDescriptorFactory;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

public class Welcome extends FragmentActivity implements
            OnMapReadyCallback,
            GoogleApiClient.ConnectionCallbacks,
            GoogleApiClient.OnConnectionFailedListener,
            LocationListener





    private GoogleMap mMap;

    //Play Service
    private static final int MY_PERMISSION_REQUEST_CODE = 7000;
    private static final int PLAY_SERVICE_RES_REQUEST = 7001;

    private LocationRequest locationRequest;
    private GoogleApiClient mGoogleApiClient;
    private Location mLastLocation;

    private static int UPDATE_INTERVAL = 5000;
    private static int FASTEST_INTERVAL = 3000;
    private static int DISPLACEMENT = 10;

    DatabaseReference drivers;
    GeoFire geoFire;

    Marker mCurrent;

    MaterialAnimatedSwitch location_switch;
    SupportMapFragment mapFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        // Obtain the SupportMapFragment and get notified when the map is ready to be used.
        mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        //Init View
        location_switch = (MaterialAnimatedSwitch)findViewById(R.id.location_switch);
        location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() 
            @Override
            public void onCheckedChanged(boolean isOnline) 
                if (isOnline)
                
                    startLocationUpdate();
                    displayLocation();
                    Snackbar.make(mapFragment.getView(),"You are online",Snackbar.LENGTH_SHORT)
                            .show();
                
                else
                
                    stopLocationUpdate();
                    mCurrent.remove();
                    Snackbar.make(mapFragment.getView(),"You are offline",Snackbar.LENGTH_SHORT)
                            .show();
                

            
        );

        //Geo Fire
        drivers = FirebaseDatabase.getInstance().getReference("Drivers");
        geoFire = new GeoFire (drivers);

        setUpLocation();
    

    //Press Ctrl+O


    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) 
        switch (requestCode)
        
            case MY_PERMISSION_REQUEST_CODE:
                if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)

                
                    if (checkPlayServices())
                    
                        buildGoogleApiClient();
                        createLocationRequest();
                        if (location_switch.isChecked())
                            displayLocation();
                    
                

        
    

    private void setUpLocation() 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        
            //Request runtime permission
            ActivityCompat.requestPermissions(this, new String[]
                    Manifest.permission.ACCESS_COARSE_LOCATION,
                    Manifest.permission.ACCESS_FINE_LOCATION
            ,MY_PERMISSION_REQUEST_CODE);
        
        else
        
           if (checkPlayServices())
           
               buildGoogleApiClient();
               createLocationRequest();
               if (location_switch.isChecked())
                   displayLocation();
           
        

    

    private void createLocationRequest() 
        LocationRequest locationRequest = LocationRequest.create();
        locationRequest.setInterval(5000);
        locationRequest.setFastestInterval(1000);
        locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);


    

    private void buildGoogleApiClient() 
        mGoogleApiClient = new GoogleApiClient.Builder(this)
                .addConnectionCallbacks(this)
                .addOnConnectionFailedListener(this)
                .addApi(LocationServices.API)
                .build();
        mGoogleApiClient.connect();
    

    private boolean checkPlayServices() 
        int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
        if (resultCode != ConnectionResult.SUCCESS)
        
            if (GooglePlayServicesUtil.isUserRecoverableError(resultCode))
                GooglePlayServicesUtil.getErrorDialog(resultCode,this,PLAY_SERVICE_RES_REQUEST).show();
            else 
                Toast.makeText(this, "This device is not supported", Toast.LENGTH_SHORT).show();
                finish();
            
            return false;
        
        return true;

    


    private void stopLocationUpdate() 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        
            return;
        
        LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient,this);
    

    private void displayLocation() 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        
            return;
        
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
        if (mLastLocation != null)
        
            if (location_switch.isChecked())
            
                final double latitude = mLastLocation.getLatitude();
                final double longitude = mLastLocation.getLongitude();

                //Update to firebase
                geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitude, longitude), new GeoFire.CompletionListener() 
                    @Override
                    public void onComplete(String key, DatabaseError error) 
                        //Add marker
                        if (mCurrent != null)
                            mCurrent.remove(); //Remove already marker
                        mCurrent = mMap.addMarker(new MarkerOptions()
                                                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
                                                    .position(new LatLng(latitude,longitude))
                                                    .title("You"));

                        // Move camera to this position
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),15.0f));
                        //Draw animation rotate marker
                        rotateMarker(mCurrent,-360,mMap);
                    
                );
            
            else
                
                    Log.d("Error", "Cannot get your location");
                
        

    

    private void rotateMarker(final Marker mCurrent, final float i, GoogleMap mMap) 
        final Handler handler = new Handler();
        final long start = SystemClock.uptimeMillis();
        final float startRotation = mCurrent.getRotation();
        final long duration = 1500;


        final Interpolator interpolator = new LinearInterpolator();

        handler.post(new Runnable() 
            @Override
            public void run() 
                long elapsed = SystemClock.uptimeMillis() - start;
                float t = interpolator.getInterpolation((float)elapsed/duration);
                float rot = t*i+(1-t)*startRotation;
                mCurrent.setRotation(-rot > 180?rot/2:rot);
                if (t<1.0)
                
                    handler.postDelayed(this,16);
                
            
        );

    

    private void startLocationUpdate() 
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
                ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED)
        
            return;
        
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,locationRequest,this);
    


    @Override
    public void onMapReady(GoogleMap googleMap) 
        mMap = googleMap;


    

    @Override
    public void onConnected(@Nullable Bundle bundle) 
        displayLocation();
        startLocationUpdate();
    

    @Override
    public void onConnectionSuspended(int i) 
        mGoogleApiClient.connect();
    

    @Override
    public void onConnectionFailed(@NonNull ConnectionResult connectionResult) 

    

    @Override
    public void onLocationChanged(Location location) 
        mLastLocation = location;
        displayLocation();
    

【问题讨论】:

【参考方案1】:

您尝试使用该行请求位置更新

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

问题是你从不初始化locationRequest 变量。你有下一个功能

private void createLocationRequest() 
    LocationRequest locationRequest = LocationRequest.create();
    locationRequest.setInterval(5000);
    locationRequest.setFastestInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

要解决您的问题,请在 createLocationRequest() 末尾添加这一行

this.locationRequest = locationRequest

【讨论】:

我尝试在 createLocationRequest() 下直接添加该行,因为当您尝试在最后添加它时,会出现错误。我仍然遇到同样的问题。如果你能告诉我你的意思,那就太好了 您可以修改createLocationRequest()如下:private void createLocationRequest() LocationRequest locationRequest = LocationRequest.create(); locationRequest.setInterval(5000); locationRequest.setFastestInterval(1000); locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); this.locationRequest = locationRequest; 或者你可以删除你的 remove locationRequest 类变量。修改createLocationRequest() 以返回LocationRequest 实例并像LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient,createLocationRequest(),this); 那样使用它 您发送的第一个修改解决了问题。我不得不在底部添加这个,一切都解决了。 现在的新问题是它在 API 22 的较低模拟器上工作。但是当我在 API 28 的模拟器上使用它时,它又崩溃了。您认为这是什么原因造成的?【参考方案2】:
private void createLocationRequest()  
LocationRequest locationRequest = LocationRequest.create();
locationRequest.setInterval(5000); 
locationRequest.setFastestInterval(1000);  
locationRequest.setPriority (LocationRequest.PRIORITY_HIGH_ACCURACY); 
this.locationRequest = locationRequest;

这就是问题的解决方案。

【讨论】:

以上是关于构建我的应用程序并安装 Google Maps API 后,每当我倾向于搜索位置时,我的应用程序就会崩溃的主要内容,如果未能解决你的问题,请参考以下文章

Swift:未安装 Google Maps 时,应用程序需要打开 Apple Maps

使用 Geolocation 和 Google Maps API [帮助]

为 Google Maps API v2 Android 发布流媒体方向

使用 Google Maps Android API 构建派对应用?

AIR - Google Maps API安全沙箱违规?

Rails 6 和 Google Maps for Rails