2015-09-25 52 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