2011-06-03 34 views
-1

在我的應用程序中,我正在檢查GPS強度。我得到一個java.lang.IllegalArgumentException我的LocationListener在Android中爲null

listener==null. 

我沒有指派代碼監聽器是空的,我寫的OnCreate()方法的代碼。我不知道爲什麼聽者會變爲空。我哪裏錯了?

我的代碼:

public class Gps_strength extends Activity{ 
    private LocationManager mLocationManager; 
    private LocationListener mGpsLocationListener; 
    private GpsStatus.Listener gps_listener; 

    public void onCreate(Bundle savedInstanceState) { 
     mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mGpsLocationListener); // line no 31. 
     mLocationManager.addGpsStatusListener(gps_listener); 
     if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER) == true) { 
      ..... GPS strength checking coding.. 
     } 
    } 
} 

的logcat:

06-03 13:04:30.695: ERROR/AndroidRuntime(2595): Caused by:  java.lang.IllegalArgumentException: listener==null 
    06-03 13:04:30.695: ERROR/AndroidRuntime(2595):  at android.location.LocationManager.requestLocationUpdates(LocationManager.java:627) 
    06-03 13:04:30.695: ERROR/AndroidRuntime(2595):  at com.gps.Gps_strength.onCreate(Gps_strength.java:31) 

回答

0

你似乎沒有設置mGpsLocationListener等於任何東西。


改變你declations到:

private LocationListener mGpsLocationListener = new LocationListener() 
{ 

    public void onLocationChanged(Location arg0) 
    { 
    } 

    public void onProviderDisabled(String provider) 
    { 
    } 

    public void onProviderEnabled(String provider) 
    { 
    } 

    public void onStatusChanged(String provider, int status, Bundle extras) 
    { 
    } 

}; 

private GpsStatus.Listener gps_listener = new GpsStatus.Listener() 
{ 
    public void onGpsStatusChanged(int event) 
    { 
    } 
}; 
+0

如何設置mGpsLocationListener的有效值而不是null – 2011-06-03 09:39:45

+0

更新了原始評論。 – 2011-06-03 10:15:03

+0

Murali你經歷了我已經回覆給你的網址 – 2011-06-03 10:22:37

0

您傳遞gps_listener作爲參數傳遞給mLocationManager.addGpsStatusListener(),但你永遠不將其分配給一個有效的對象,所以它被設置爲null

+0

我設置爲mLocationManager.addGpsStatusListener(gps_listener);在代碼中也。我發佈我的代碼第一。請協助我。 – 2011-06-03 09:13:39

+0

如果您不需要在GPS狀態更改時收到通知,請將'mLocationManager.addGpsStatusListener(gps_listener);'註釋掉。 – 2011-06-03 09:20:59

+0

我在'mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,0,0,mGpsLocationListener);'註釋掉'mLocationManager.addGpsStatusListener(gps_listener);' – 2011-06-03 09:30:35

0

你已經寫private GpsStatus.Listener gps_listener;以上statemnets是相同private GpsStatus.Listener gps_listener = null; 但在你的代碼中無處gps_listener值賦給顯示。直接將gps_listener指定爲mLocationManager.addGpsStatusListener(gps_listener);

先指定再使用。

您可以在這裏找到示例代碼 http://hejp.co.uk/android/android-gps-example/

How can I check the current status of the GPS receiver?

感謝 迪帕克

+0

感謝如何爲gps_listener設置有效值而不是null。請指導我。 – 2011-06-03 09:11:43

+0

現在看到我的迴應。看看這兩個網址。 – 2011-06-03 09:27:54

相關問題