2012-11-09 49 views
1

我正在構建一個利用用戶地理位置的應用程序。這個想法是,當他們導航到DataEntry活動時,應用程序獲取用戶位置並用String值(從原始雙lat/lng值轉換)填充TextView。我很難讓它工作,但看不到我要出錯的地方。在我嘗試獲取位置的地方加載DE.Java活動時,出現NullPointerException錯誤。讓我相信位置值尚未初始化,但對我來說它看起來像它!產生位置值的錯誤android

這裏是DE.java

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_data_entry); 

    txtBuildingName=(EditText)this.findViewById(R.id.nameContent); 
    txtDescription=(EditText)this.findViewById(R.id.descriptionContent); 

    txtLongitude=(TextView)this.findViewById(R.id.TextViewLong); 
    txtLongitude.setText(getLongitude()); 

    project=(Spinner)findViewById(R.id.type_project); 
    ArrayAdapter<CharSequence> adapterTwo = ArrayAdapter.createFromResource(this, R.array.type_project_array, android.R.layout.simple_spinner_item); 
    project.setAdapter(adapterTwo); 

相關的代碼,而對於getLongitude()方法的代碼:

 public String getLongitude(){ 
    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    longitude = location.getLongitude(); 
    String longRes = String.valueOf(longitude); 
    return longRes; 
} 

現在的應用還處於發展所以現在我已經打開了GPS並在命令行中使用telnet連接和geo fix命令輸入了一些位置值,這就是爲什麼我沒有執行任何LocationProvider的檢查,但我將在未來。目前,人們可以假定GPS始終可用。

下面是logcat的日誌prinout:

11-09 14:55:17.301: E/AndroidRuntime(546): FATAL EXCEPTION: main 
11-09 14:55:17.301: E/AndroidRuntime(546): java.lang.RuntimeException: Unable to start activity ComponentInfo{application.prototype.mfb/application.prototype.mfb.DataEntry}: java.lang.NullPointerException 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.os.Handler.dispatchMessage(Handler.java:99) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.os.Looper.loop(Looper.java:137) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread.main(ActivityThread.java:4424) 
11-09 14:55:17.301: E/AndroidRuntime(546): at java.lang.reflect.Method.invokeNative(Native Method) 
11-09 14:55:17.301: E/AndroidRuntime(546): at java.lang.reflect.Method.invoke(Method.java:511) 
11-09 14:55:17.301: E/AndroidRuntime(546): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
11-09 14:55:17.301: E/AndroidRuntime(546): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
11-09 14:55:17.301: E/AndroidRuntime(546): at dalvik.system.NativeStart.main(Native Method) 
11-09 14:55:17.301: E/AndroidRuntime(546): Caused by: java.lang.NullPointerException 
11-09 14:55:17.301: E/AndroidRuntime(546): at application.prototype.mfb.DataEntry.getLongitude(DataEntry.java:181) 
11-09 14:55:17.301: E/AndroidRuntime(546): at application.prototype.mfb.DataEntry.onCreate(DataEntry.java:54) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.Activity.performCreate(Activity.java:4465) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049) 
11-09 14:55:17.301: E/AndroidRuntime(546): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920) 
11-09 14:55:17.301: E/AndroidRuntime(546): ... 11 more 

我已經添加在manifest.xml文件的正確ACCESS_FINE_LOCATION和「ACCESS_COARSE_LOCATION`以及這樣就不會成爲問題。

任何想法?謝謝。

回答

1

這一行:

Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

位置可能爲空,你必須檢查空!

+0

但位置如何可以爲空,如果我使用telnet和geofix在命令行設置值? – WebDevDanno

+0

請閱讀方法說明:'如果提供者當前被禁用,則返回空值。' – Budius

+0

我意識到這一點,但我已經啓用了AVD上的GPS支持,我已經使用telnet和geofix輸入了一些緯度/經度值。它仍然返回null。 – WebDevDanno