如何修复初始化时位置管理器崩溃的应用程序



我有一些代码使用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) 

您的例外情况是:

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 文档中了解有关 Java 接口的更多信息。

最新更新