2016-04-05 11 views
0

Ich brauche den aktuellen Standort mit separaten Klasse, die nicht in der Aktivität. Das ist mein Code und es funktioniert nicht. Jeder hat eine Idee, dies zu beheben. Diese Anwendung stürzt immer ab, wenn ich versuche, Details zum Standort zu erhalten.So erhalten Sie den aktuellen Standort mit separaten Klasse in Android

package com.example.ishanfx.departmentapp.network; 

import android.content.Context; 
import android.location.Criteria; 
import android.location.Location; 
import android.location.LocationListener; 
import android.location.LocationManager; 
import android.os.Bundle; 

import com.google.android.gms.common.ConnectionResult; 
import com.google.android.gms.common.api.GoogleApiClient; 
import com.google.android.gms.location.LocationRequest; 
import com.google.android.gms.location.LocationServices; 


public class LocationHandler implements LocationListener,GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener { 
    private LocationManager locationManager; 
    Location mLastLocation; 
    LocationRequest mLocationRequest; 
    private String latitude; 
    private String longitude; 
    Context context; 
    private static GoogleApiClient mGoogleApiClient; 

    public LocationHandler(Context context) { 
     this.context = context; 
     locationManager = (LocationManager) context 
       .getSystemService(Context.LOCATION_SERVICE); 
     buildGoogleApiClient(); 


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

    } 
    private void setMostRecentLocation(Location lastKnownLocation) { 

    } 

    public String getLatitude() { 
     return latitude; 
    } 

    public String getLongitude() { 
     return longitude; 
    } 


    @Override 
    public void onLocationChanged(Location location) { 
     double lon = (double) (location.getLongitude()); 
     double lat = (double) (location.getLatitude()); 

     latitude = lat + ""; 
     longitude = lon + ""; 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * android.location.LocationListener#onProviderDisabled(java.lang.String) 
    */ 
    @Override 
    public void onProviderDisabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 

    /* 
    * (non-Javadoc) 
    * 
    * @see 
    * android.location.LocationListener#onProviderEnabled(java.lang.String) 
    */ 
    @Override 
    public void onProviderEnabled(String arg0) { 
     // TODO Auto-generated method stub 

    } 


    @Override 
    public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 
     // TODO Auto-generated method stub 

    } 

    @Override 
    public void onConnected(Bundle bundle) { 
     mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
       mGoogleApiClient); 
     if (mLastLocation != null) { 
      latitude =String.valueOf(mLastLocation.getLatitude()); 
      longitude = String.valueOf(mLastLocation.getLongitude()); 
     } 

    } 

    @Override 
    public void onConnectionSuspended(int i) { 

    } 

    @Override 
    public void onConnectionFailed(ConnectionResult connectionResult) { 

    } 
} 

Dies ist die mainActivity.When ich diese Anwendung aufrufen wird abstürzen.

LocationHandler appLocationManager = new LocationHandler(HomeActivity.this); 

       Toast.makeText(getApplicationContext(), appLocationManager.getLatitude().toString(), Toast.LENGTH_SHORT).show(); 
       Toast.makeText(getApplicationContext(), appLocationManager.getLongitude().toString(), Toast.LENGTH_SHORT).show(); 

Dies ist das Log

FATAL EXCEPTION: main 
                       java.lang.NullPointerException 
                        at com.example.ishanfx.departmentapp.HomeActivity.onOptionsItemSelected(HomeActivity.java:172) 
                        at android.app.Activity.onMenuItemSelected(Activity.java:2502) 
                        at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:361) 
                        at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:147) 
                        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100) 
                        at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100) 
                        at android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:68) 
                        at android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:172) 
                        at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:760) 
                        at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:811) 
                        at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152) 
                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:958) 
                        at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:948) 
                        at android.support.v7.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:191) 
                        at android.widget.AdapterView.performItemClick(AdapterView.java:292) 
                        at android.widget.AbsListView.performItemClick(AbsListView.java:1065) 
                        at android.widget.AbsListView$PerformClick.run(AbsListView.java:2522) 
                        at android.widget.AbsListView$1.run(AbsListView.java:3183) 
                        at android.os.Handler.handleCallback(Handler.java:605) 
                        at android.os.Handler.dispatchMessage(Handler.java:92) 
                        at android.os.Looper.loop(Looper.java:137) 
                        at android.app.ActivityThread.main(ActivityThread.java:4441) 
                        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:784) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
                        at dalvik.system.NativeStart.main(Native Method) 
+0

Was ist der Logcat Ausgang? – chRyNaN

+0

Nullpointer Ausnahme @chRyNaN –

+0

Setzen Sie die ganze Protokolle in Ihre Frage. Hier ist ein Link zu beantworten, was eine NullPointerException ist, http://StackOverflow.com/q/218384/1478764 – chRyNaN

Antwort

1

es ein wenig Zeit für die onConnected Ereignis ausgelöst nimmt. Sie müssen es ein wenig Zeit geben, bevor Sie Ihren Toast machen. würde ich eine isConnected Eigenschaft auf Ihre LocationHandler Klasse hinzufügen wie folgt:

private boolean isconnected = false; 

public boolean isConnected() { 
    return isconnected; 
} 

@Override 
public void onConnected(Bundle bundle) { 
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
      mGoogleApiClient); 
    if (mLastLocation != null) { 
     latitude =String.valueOf(mLastLocation.getLatitude()); 
     longitude = String.valueOf(mLastLocation.getLongitude()); 
    } 
    this.isconnected = true; 
} 

Dann in MainActivity

LocationHandler appLocationManager = new LocationHandler(HomeActivity.this); 

while(!appLocationManager.isConnected()) { 
// wait for a bit 
} 

if (appLocationManager.isConnected()) { 
    Toast.makeText(getApplicationContext(), appLocationManager.getLatitude().toString(), Toast.LENGTH_SHORT).show(); 
    Toast.makeText(getApplicationContext(), appLocationManager.getLongitude().toString(), Toast.LENGTH_SHORT).show(); 
} 
+0

Es die App einfrieren. –

+0

Irgendeine Idee, es im Hintergrundfaden zu laufen? –

+0

erstellen Sie eine Schnittstelle (Callback) und wenn es verbunden ist, gibt es zurück. appLocationManager .setOnConnectedListener (new OnConnected() {}) – Nepster

1

Ich habe oben Klasse in eine saubere Art und Weise aktualisiert.

public class LocationHandler implements LocationListener, 
     GoogleApiClient.ConnectionCallbacks, 
     GoogleApiClient.OnConnectionFailedListener { 
    private Context mContext; 
    private GoogleApiClient mGoogleApiClient; 
    private LocationRequest mLocationRequest; 
    private OnLocationUpdateListener onLocationUpdateListener; 

    public LocationHandler(Context mContext) { 
     this.mContext = mContext; 
     buildGoogleApiClient(); 
     createLocationRequest(); 
    } 

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

    public void startLocationUpdates(Context mContext) { 
     if (ActivityCompat.checkSelfPermission(mContext, 
       Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED 
       && ActivityCompat.checkSelfPermission(mContext, 
       Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
      return; 
     } 
     LocationServices.FusedLocationApi.requestLocationUpdates(
       mGoogleApiClient, mLocationRequest, this); 
    } 

    private void stopLocationUpdate(Context mContext) { 
     LocationServices.FusedLocationApi.removeLocationUpdates(
       mGoogleApiClient, this); 
    } 

    //other new Methods but not using right now.. 
    protected void createLocationRequest() { 
     mLocationRequest = new LocationRequest(); 
     mLocationRequest.setInterval(10000);//set the interval in which you want to get locations 
     mLocationRequest.setFastestInterval(5000);//if a location is available sooner you can get it (i.e. another app is using the location services) 
     mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY); 
    } 

    @Override 
    public void onConnected(@Nullable Bundle bundle) { 
     startLocationUpdates(mContext); 
    } 

    @Override 
    public void onConnectionSuspended(int i) { 
    } 

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

    @Override 
    public void onLocationChanged(Location location) { 
     if(mGoogleApiClient.isConnected() && onLocationUpdateListener != null){ 
      onLocationUpdateListener.onLocationChange(location); 
     } 
    } 

    public void setOnLocationUpdateListener(OnLocationUpdateListener onLocationUpdateListener) { 
     this.onLocationUpdateListener = onLocationUpdateListener; 
    } 
} 

wo OnLocationUpdateListener ist

public interface OnLocationUpdateListener { 
    void onLocationChange(Location location); 
    void onError(EnumUtil.ErrorType errorType); 
}