0

i'm currently working on android maps by following this tutorial http://developer.android.com/training/location/retrieve-current.html, and i keep getting error.there are 4 warnings and i tried to fix it but when i run it again the error message fatal exception still showing

here's the warning messages: 1. the message servicesConnected() from the type LokasiKampus is never used locally 2. the value of local variable kampus is not used 3. the value of local variable laboratorium is not used 4.the value of local variable myCurrentLocation is not used

here's my Java code

package ninth.example.dteinformationcenter;
import android.app.Activity;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.Intent;
import android.content.IntentSender;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.widget.Toast;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.GooglePlayServicesClient;
import com.google.android.gms.common.GooglePlayServicesUtil;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.MapView;
import com.google.android.gms.maps.CameraUpdateFactory;
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;


public class LokasiKampus extends FragmentActivity implements 
GooglePlayServicesClient.ConnectionCallbacks,
GooglePlayServicesClient.OnConnectionFailedListener{
static final LatLng Kampus = new LatLng (-7.775038, 110.373253);
static final LatLng Laboratorium = new LatLng (-7.765437, 110.374496);
private static final int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;
private GoogleMap map;
private GooglePlayServicesClient myLocationClient;


public static class ErrorDialogFragment extends DialogFragment {
    private Dialog mDialog;
    public ErrorDialogFragment() {
        super();
        mDialog = null;
    }
    public void setDialog(Dialog dialog) {
        mDialog = dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return mDialog;
    }
    public void show(FragmentManager supportFragmentManager, String tag) {
        // TODO Auto-generated method stub

    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch(requestCode) {
    case CONNECTION_FAILURE_RESOLUTION_REQUEST :
        switch(resultCode) {
        case Activity.RESULT_OK : break;
        }
    }
}

**private boolean servicesConnected()** {
    int resultCode = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
    if (ConnectionResult.SUCCESS == resultCode) {
        Log.d("Location Updated", "Google Play Services is available");
    return true;
    } else {

        Dialog ErrorDialog = GooglePlayServicesUtil
                .getErrorDialog(resultCode, this, CONNECTION_FAILURE_RESOLUTION_REQUEST);

        if (ErrorDialog != null) {
            ErrorDialogFragment errorFragment = new ErrorDialogFragment();

            errorFragment.setDialog(ErrorDialog);
            errorFragment.show(getSupportFragmentManager(), "Location Updates");
        }
        return false;
    }
}


protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.lokasi_kampus);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.lokasikampus)).getMap();

    if (map != null) {
        **Marker kampus = map.addMarker(new MarkerOptions().position(Kampus)
                .title("Kampus "));
        Marker laboratorium = map.addMarker(new MarkerOptions().position(Laboratorium)
                .position(Laboratorium)
                .title("Laboratorium")
                );**

        map.moveCamera(CameraUpdateFactory.newLatLngZoom(Kampus, 18));
        map.animateCamera(CameraUpdateFactory.zoomTo(10), 2000, null);
    }

    LocationClient myLocationClient  = new LocationClient(this,this,this);

    **Location myCurrentLocation;
    myCurrentLocation = myLocationClient.getLastLocation();**
}

@Override
public void onConnected(Bundle dataBundle) {
    Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}

@Override
public void onDisconnected() {
    Toast.makeText(this, "Disconnected, please re-connect", Toast.LENGTH_SHORT).show();
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    if(connectionResult.hasResolution()) {
        try {
            connectionResult.startResolutionForResult(this, CONNECTION_FAILURE_RESOLUTION_REQUEST);
        } catch(IntentSender.SendIntentException e) {
            e.printStackTrace();
        }
    } else {
        Toast.makeText(this, "Connection Failed", Toast.LENGTH_SHORT).show();
    }
}

@Override
protected void onStart() {
    super.onStart();
    myLocationClient.connect();
        }

@Override
protected void onStop() {
    myLocationClient.disconnect();
    super.onStop();
}

}

this is the error message

FATAL EXCEPTION: main
 Process: ninth.example.dteinformationcenter, PID: 1443
java.lang.RuntimeException: Unable to start activity ComponentInfo{ninth.example.dteinformationcenter/ninth.example.dteinformationcenter.LokasiKampus}: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.IllegalStateException: Not connected. Call connect() and wait for onConnected() to be called.
at com.google.android.gms.internal.hb.cn(Unknown Source)
at com.google.android.gms.internal.jg.b(Unknown Source)
at com.google.android.gms.internal.jg$c.cn(Unknown Source)
at com.google.android.gms.internal.jf.getLastLocation(Unknown Source)
at com.google.android.gms.internal.jg.getLastLocation(Unknown Source)
at com.google.android.gms.location.LocationClient.getLastLocation(Unknown Source)
at ninth.example.dteinformationcenter.LokasiKampus.onCreate(LokasiKampus.java:112)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
  • Possibly duplicate :http://stackoverflow.com/questions/21107237/not-connected-call-connect-or-wait-for-onconnected-to-be-called – Haresh Chhelana Nov 25 '14 at 11:11
  • call `servicesConnected();` in your oncreate. – M S Gadag Nov 25 '14 at 11:11
  • You are using LocationClient which requires connection to google services, so any request for location needs to wait until connection is establish so move this `Location myCurrentLocation; myCurrentLocation = myLocationClient.getLastLocation();` inside `onConnected(Bundle)` you also need to call `locationClient.connect();` when your activity starts. Plus, You might also need to implement LocationListener. – j4rey Nov 25 '14 at 11:26
  • i followed your instruction but still getting error, and the PID changed to PID:938 – faisal ali Nov 25 '14 at 11:56

0 Answers0