Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/205.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法获取android设备的当前位置_Android_Gps - Fatal编程技术网

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

无法获取android设备的当前位置,android,gps,Android,Gps,你好,我正试图写一个天气应用程序,我想得到当前的位置。我只想得到手机位置的当前纬度和经度。 我试着作为一个新班级来做。但还是不成功。 但我的应用程序总是崩溃。 这是我的密码 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKn

你好,我正试图写一个天气应用程序,我想得到当前的位置。我只想得到手机位置的当前纬度和经度。 我试着作为一个新班级来做。但还是不成功。 但我的应用程序总是崩溃。 这是我的密码

    @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

下一行为变量指定了错误的类型:

LocationListener mlocListener = new MyLocationListener();
请尝试使用此选项:

MyLocationListener mlocListener = new MyLocationListener();

张贴你的堆栈trace@fractalwrench添加了堆栈跟踪