2

I'm really a newbie so I just watch tutorials. I'm having an issue about the location. I'm using a switch to go online/offline but when I pressed the switch, my app crashes.Here is the code

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

private GoogleMap mMap;

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

private LocationRequest mLocationRequest;
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)
            {
                StartLocationUpdates();
                displayLocation();
                Snackbar.make(mapFragment.getView(),"You are online",Snackbar.LENGTH_SHORT).show();
            }
            else
            {
                stopLocationUpdates();
                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();
}

@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(checkedPlayServices())
            {
                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(checkedPlayServices())
        {
            buildGoogleApiClient();
            createLocationRequest();
            if(location_switch.isChecked())
            {
                displayLocation();
            }

        }

    }
}

private void createLocationRequest() {
    mLocationRequest = new LocationRequest();
    mLocationRequest.setInterval(UPDATE_INTERVAL);
    mLocationRequest.setFastestInterval(FASTEST_INTERVAL);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    mLocationRequest.setSmallestDisplacement(DISPLACEMENT);


}

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

private boolean checkedPlayServices() {
    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 stopLocationUpdates() {
    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

                    mCurrent = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude))
                            .title("You are here!").snippet("Consider yourself located")
                            .icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));

                        //Move camera to this position
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitude,longitude),15.0f));

                        //Draw animation rotate marker
                        rotateMarker(mCurrent,-360,mMap);

                    if(mCurrent != null) {

                        mCurrent.remove(); // Remove marker
                    }

                }
            });
        }
    }
    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 StartLocationUpdates() {
    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,mLocationRequest,this);
}

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

}

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


}

//@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 onConnected(@Nullable Bundle bundle) {
    displayLocation();
    StartLocationUpdates();

}

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

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}

}

I tried to debug it and it always points out the mCurrent = mMap.addMarker part. I tried a lot but it always crashes. Please help me fix it. Thankyou

also this is the logcat

01-28 06:15:29.817 5620-5739/com.judemark.triber.driverapp E/MPlugin: Unsupported class: com.mediatek.common.telephony.IOnlyOwnerSimSupport
01-28 06:15:30.126 5620-5620/com.judemark.triber.driverapp E/Minikin: addFont failed to create font /system/fonts/Padauk.ttf
01-28 06:15:30.248 5620-5620/com.judemark.triber.driverapp E/Minikin: addFont failed to create font /system/fonts/NanumGothic.ttf
01-28 06:15:30.251 5620-5620/com.judemark.triber.driverapp E/Minikin: addFont failed to create font /system/fonts/DroidSansFallback.ttf
01-28 06:15:30.251 5620-5620/com.judemark.triber.driverapp E/Minikin: addFont failed to create font /system/fonts/MTLmr3m.ttf
01-28 06:15:30.597 5620-5620/com.judemark.triber.driverapp E/MultiWindowProxy: getServiceInstance failed!
01-28 06:15:31.138 5620-5798/com.judemark.triber.driverapp E/GED: Failed to get GED Log Buf, err(0)
01-28 06:15:31.616 5620-5800/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f765eb680 cert_verify_callback x509_store_ctx=0x7f618579f0 arg=0x0
01-28 06:15:31.616 5620-5800/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f765eb680 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
01-28 06:16:01.626 5620-5620/com.judemark.triber.driverapp E/MultiWindowProxy: getServiceInstance failed!
01-28 06:16:02.729 5620-7093/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f65a74b00 cert_verify_callback x509_store_ctx=0x7f49332220 arg=0x0
01-28 06:16:02.729 5620-7093/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f65a74b00 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
01-28 06:16:04.864 5620-7095/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f5e659d80 cert_verify_callback x509_store_ctx=0x7f49128260 arg=0x0
01-28 06:16:04.865 5620-7095/com.judemark.triber.driverapp E/NativeCrypto: ssl=0x7f5e659d80 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
01-28 06:16:07.272 5620-5620/com.judemark.triber.driverapp E/UncaughtException: com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap.
                                                                                at com.google.maps.api.android.lib6.impl.m.a(:com.google.android.gms.DynamiteModulesB@11975448:12)
                                                                                at com.google.maps.api.android.lib6.impl.n.a(:com.google.android.gms.DynamiteModulesB@11975448:7)
                                                                                at com.google.maps.api.android.lib6.impl.cz.<init>(:com.google.android.gms.DynamiteModulesB@11975448:25)
                                                                                at com.google.maps.api.android.lib6.impl.ba.a(:com.google.android.gms.DynamiteModulesB@11975448:487)
                                                                                at com.google.android.gms.maps.internal.k.onTransact(:com.google.android.gms.DynamiteModulesB@11975448:94)
                                                                                at android.os.Binder.transact(Binder.java:392)
                                                                                at com.google.android.gms.internal.zzeu.zza(Unknown Source)
                                                                                at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source)
                                                                                at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                                                                                at com.judemark.triber.driverapp.Welcome$2.onComplete(Welcome.java:213)
                                                                                at com.firebase.geofire.GeoFire$2.onComplete(GeoFire.java:175)
                                                                                at com.google.android.gms.internal.zzeek.run(Unknown Source)
                                                                                at android.os.Handler.handleCallback(Handler.java:815)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                                at android.os.Looper.loop(Looper.java:207)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
01-28 06:16:07.508 5620-5620/com.judemark.triber.driverapp E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: com.judemark.triber.driverapp, PID: 5620
                                                                         com.google.maps.api.android.lib6.common.apiexception.b: Failed to decode image. The provided image must be a Bitmap.
                                                                             at com.google.maps.api.android.lib6.impl.m.a(:com.google.android.gms.DynamiteModulesB@11975448:12)
                                                                             at com.google.maps.api.android.lib6.impl.n.a(:com.google.android.gms.DynamiteModulesB@11975448:7)
                                                                             at com.google.maps.api.android.lib6.impl.cz.<init>(:com.google.android.gms.DynamiteModulesB@11975448:25)
                                                                             at com.google.maps.api.android.lib6.impl.ba.a(:com.google.android.gms.DynamiteModulesB@11975448:487)
                                                                             at com.google.android.gms.maps.internal.k.onTransact(:com.google.android.gms.DynamiteModulesB@11975448:94)
                                                                             at android.os.Binder.transact(Binder.java:392)
                                                                             at com.google.android.gms.internal.zzeu.zza(Unknown Source)
                                                                             at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source)
                                                                             at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
                                                                             at com.judemark.triber.driverapp.Welcome$2.onComplete(Welcome.java:213)
                                                                             at com.firebase.geofire.GeoFire$2.onComplete(GeoFire.java:175)
                                                                             at com.google.android.gms.internal.zzeek.run(Unknown Source)
                                                                             at android.os.Handler.handleCallback(Handler.java:815)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:104)
                                                                             at android.os.Looper.loop(Looper.java:207)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5769)
                                                                             at java.lang.reflect.Method.invoke(Native Method)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
01-28 06:16:08.644 5620-7155/? E/NativeCrypto: ssl=0x7f457f5200 cert_verify_callback x509_store_ctx=0x7f35dbe350 arg=0x0
01-28 06:16:08.644 5620-7155/? E/NativeCrypto: ssl=0x7f457f5200 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_ECDSA
shindou ai
  • 11
  • 5
  • 1
    Read [this question and its answers](http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this) for tips on how to figure out why your app crashes. – Code-Apprentice Jan 27 '18 at 21:20
  • @Code-Apprentice I tried sir. in the logcat, its points out this part mCurrent = mMap.addMarker(new MarkerOptions().position(new LatLng(latitude,longitude)) .title("You are here!").snippet("Consider yourself located") .icon(BitmapDescriptorFactory.fromResource(R.drawable.car))); – shindou ai Jan 27 '18 at 21:31
  • Please [edit] your question to add these details. – Code-Apprentice Jan 27 '18 at 21:33
  • I put it in the bottom part sir. – shindou ai Jan 27 '18 at 21:35
  • 1. Please make the code which causes the error more obvious by formatting it. 2. Show the error from the logcat. – Code-Apprentice Jan 27 '18 at 21:39
  • at com.google.android.gms.maps.internal.zzg.addMarker(Unknown Source) at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source) – shindou ai Jan 27 '18 at 22:07
  • That is only a single line from the entire stacktrace. And is not very helpful since it's from the Google Maps API. Please [edit] your question to show the entire stack trace. The first line with the exact error type is the most important as well as the line in your code where the error starts. – Code-Apprentice Jan 27 '18 at 22:22
  • logcat already in the code section sir – shindou ai Jan 27 '18 at 22:38
  • The error message seems clear. You must provide a Bitmap – Code-Apprentice Jan 27 '18 at 22:40
  • I already put a bitmap sir car icon and even if I use the default icon it doesn't show up – shindou ai Jan 27 '18 at 22:44
  • I changed the icon and it zooms in my location, but still even with the default code from google, the GPS icon doesn't show up. please help – shindou ai Jan 27 '18 at 22:49

0 Answers0