如何在初始化时修复LocationManager崩溃的应用

时间:2019-03-23 23:24:11

标签: java android

我有一些使用LocationManager通过GPS查找位置的代码。每当我尝试在手机(阿尔卡特OneTouch Fierce XL)上运行它时,我都会立即收到通知“不幸(我的应用程序名称)已停止”。只有在应用程序的智能手机上启用了位置服务后,此错误才会正常运行。

我尝试了可以​​使用的任何示例代码,但结果却相同。

这是我的代码:

初始化GPS:

 locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if (ActivityCompat.checkSelfPermission(this, permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            // TODO: Consider calling
            //    ActivityCompat#requestPermissions
            // here to request the missing permissions, and then overriding
            //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
            //                                          int[] grantResults)
            // to handle the case where the user grants the permission. See the documentation
            // for ActivityCompat#requestPermissions for more details.
            return;
        }
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

我使用的方法:

 //Location stuff
    public void onLocationChanged(Location location) {
        Toast.makeText(this, "Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude(), Toast.LENGTH_LONG);
        try {
            wait(5000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void onProviderDisabled(String provider) {
        Log.d("Latitude","disable");
    }

    public void onProviderEnabled(String provider) {
        Log.d("Latitude","enable");
    }

    public void onStatusChanged(String provider, int status, Bundle extras) {
        Log.d("Latitude","status");
    }

堆栈跟踪:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.eas, PID: 11330
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.eas/com.example.eas.MainActivity}: java.lang.ClassCastException: com.example.eas.MainActivity cannot be cast to android.location.LocationListener
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2365)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2427)
    at android.app.ActivityThread.access$800(ActivityThread.java:156)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:135)
    at android.app.ActivityThread.main(ActivityThread.java:5333)
    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:940)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735)
 Caused by: java.lang.ClassCastException: com.example.eas.MainActivity cannot be cast to android.location.LocationListener
    at com.example.eas.MainActivity.onCreate(MainActivity.java:50)
    at android.app.Activity.performCreate(Activity.java:6036)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1109)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2318)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2427) 
    at android.app.ActivityThread.access$800(ActivityThread.java:156) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1331) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5333) 
    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:940) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:735) 

1 个答案:

答案 0 :(得分:2)

您的例外是:

Caused by: java.lang.ClassCastException: com.example.eas.MainActivity cannot be cast to android.location.LocationListener

它似乎来自:

locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, (LocationListener) this);

this(这似乎是您的活动)未实现LocationListener接口。添加该接口,并确保已实现该接口所需的所有方法。

您可以在Oracle's documentation中了解有关Java接口的更多信息。