无法获取Android设备的当前位置

时间:2015-09-25 15:27:42

标签: android gps

您好我正在尝试编写天气应用程序,我想获取当前位置。我只想获得手机位置的当前纬度和经度。 我尝试将它作为一个新类。但仍然没有成功。 但我的应用程序总是崩溃。 这是我的代码。

    @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

1 个答案:

答案 0 :(得分:1)

以下行指定变量的错误类型:

LocationListener mlocListener = new MyLocationListener();

请尝试使用此代码:

MyLocationListener mlocListener = new MyLocationListener();