我正在构建一个使用用户地理位置的应用。这个想法是,当他们导航到DataEntry活动时,应用程序获取用户位置并使用String值填充TextView(从原始的double lat / lng值转换)。我很难让它上班但却看不到我出错的地方。当我在尝试获取位置的地方加载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命令输入了一些位置值,这就是为什么我没有对{{1但是我将来会的。目前,人们可以假设GPS始终可用。
以下是LOGCAT日志的打印输出:
LocationProvider
我在Manifest.XML文件中添加了正确的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_COARSE_LOCATION`,这样就不会出现问题了。
有什么想法吗?谢谢。
答案 0 :(得分:1)
这一行:
Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
位置可能为null,您必须检查null!
答案 1 :(得分:0)
如果你使用sdk的vm作为模拟器。
telnet localhost 5554
然后通过
设置模拟器的位置geo fix <longitude value> <latitude value>
我在github上有一些你可以使用的代码
https://github.com/billmcnamara/BarcelonaTravelGuide.Android