我正在构建一个利用用户地理位置的应用程序。这个想法是,当他们导航到 DataEntry 活动时,应用程序会获取用户的位置并用字符串值填充 TextView(从原始的双纬度/经度值转换而来)。我很难让它工作,但看不出哪里出错了。当我在尝试获取位置的位置加载 DE.Java 活动时出现 NullPointerException 错误。让我相信位置值尚未初始化,但对我来说它看起来已经初始化了!
这是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 日志的打印输出:
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
我也在ACCESS_FINE_LOCATION
Manifest.XML 文件中添加了正确的和“ACCESS_COARSE_LOCATION”,这样就不会出现问题了。
有任何想法吗?谢谢你。