簡體   English   中英

如何在初始化時修復LocationManager崩潰的應用

[英]How to fix LocationManager crashing app on initialization

我有一些使用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接口的更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM