gpt4 book ai didi

android - 生成位置值android时出错

转载 作者:行者123 更新时间:2023-11-30 03:56:13 24 4
gpt4 key购买 nike

我正在构建一个利用用户地理位置的应用程序。这个想法是,当他们导航到 DataEntry Activity 时,应用程序会获取用户位置并使用字符串值(从原始双纬度/经度值转换而来)填充 TextView。我很难让它工作,但看不出我哪里出错了。当我在尝试获取位置的位置加载 DE.Java Activity 时出现 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

我还在 Manifest.XML 文件中添加了正确的 ACCESS_FINE_LOCATION 和“ACCESS_COARSE_LOCATION”,这样就不会出现问题了。

有什么想法吗?谢谢。

最佳答案

这一行:

位置 location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);

位置可能为空,您必须检查是否为空!

关于android - 生成位置值android时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13310793/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com