2015-09-25 67 views
-1

您好,我正在嘗試編寫一個天氣應用程序,我想獲取當前位置。我只想獲取手機位置的當前緯度和經度。 我試過把它當成一個新班級。但仍然不成功。 但我的應用程序總是崩潰。 這是我的代碼。無法獲取Android設備的當前位置

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    ButterKnife.bind(this); 
    mProgressBar.setVisibility(View.INVISIBLE); 

    /* Use the LocationManager class to obtain GPS locations */ 
    LocationManager mlocManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 

    LocationListener mlocListener = new MyLocationListener(); 
//I also get an error here about requestUpdateLocatio 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (android.location.LocationListener) mlocListener); 

    //Latitude and Longitude values. 
    final double latitude = 43.768; 
    final double longitude = -79.345; 

    mRefreshImageView.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      getForecast(latitude, longitude); 
     } 
    }); 

    getForecast(latitude, longitude); 

} 

現在我只想爲我的經緯度敬酒。但我的應用程序崩潰。

public class MyLocationListener implements LocationListener 
    { 

     @Override 
     public void onLocationChanged(Location loc) 
     { 

      loc.getLatitude(); 
      loc.getLongitude(); 

      String Text = "My current location is: " + 
        "Latitud = " + loc.getLatitude() + 
        "Longitud = " + loc.getLongitude(); 

      Toast.makeText(getApplicationContext(), Text, Toast.LENGTH_SHORT).show(); 
     } 

     public void onProviderDisabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Disabled", Toast.LENGTH_SHORT).show(); 
     } 


     public void onProviderEnabled(String provider) 
     { 
      Toast.makeText(getApplicationContext(), "Gps Enabled", Toast.LENGTH_SHORT).show(); 
     } 

     public void onStatusChanged(String provider, int status, Bundle extras) 
     { 

     } 
    } 

我所有的需要的權限 這裏是我的堆棧跟蹤?

09-25 11:38:59.863 9939-9939/? I/art﹕ Late-enabling -Xcheck:jni 
09-25 11:39:00.039 9939-9939/com.gkmohit.unknown.weatherme D/AndroidRuntime﹕ Shutting down VM 
09-25 11:39:00.044 9939-9939/com.gkmohit.unknown.weatherme E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.gkmohit.unknown.weatherme, PID: 9939 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gkmohit.unknown.weatherme/com.gkmohit.unknown.weatherme.UI.MainActivity}: java.lang.ClassCastException: com.gkmohit.unknown.weatherme.UI.MainActivity$MyLocationListener cannot be cast to android.location.LocationListener 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
      at android.app.ActivityThread.access$800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:135) 
      at android.app.ActivityThread.main(ActivityThread.java:5254) 
      at java.lang.reflect.Method.invoke(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:372) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
    Caused by: java.lang.ClassCastException: com.gkmohit.unknown.weatherme.UI.MainActivity$MyLocationListener cannot be cast to android.location.LocationListener 
      at com.gkmohit.unknown.weatherme.UI.MainActivity.onCreate(MainActivity.java:78) 
      at android.app.Activity.performCreate(Activity.java:5990) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
            at android.app.ActivityThread.access$800(ActivityThread.java:151) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:135) 
            at android.app.ActivityThread.main(ActivityThread.java:5254) 
            at java.lang.reflect.Method.invoke(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:372) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
09-25 11:39:04.649 9939-9939/? I/Process﹕ Sending signal. PID: 9939 SIG: 9 
+0

安置自己的堆棧跟蹤 – fractalwrench

+0

@fractalwrench添加的堆棧跟蹤 – gkmohit

回答

1

以下行指定了錯誤類型的變量:

LocationListener mlocListener = new MyLocationListener(); 

使用這個,而不是嘗試:

MyLocationListener mlocListener = new MyLocationListener(); 
+0

不,仍然是同樣的錯誤。 – gkmohit