使用GPS的Android应用程序中的NullPointerException
我有一个非常简单的类,它查找最后一个已知的用户坐标,然后打印其中一个。但是我把loc作为空变量。应用程序未将数据读入loc变量使用GPS的Android应用程序中的NullPointerException,android,gps,nullpointerexception,Android,Gps,Nullpointerexception,我有一个非常简单的类,它查找最后一个已知的用户坐标,然后打印其中一个。但是我把loc作为空变量。应用程序未将数据读入loc变量 import android.app.Activity; import android.content.Context; import android.location.Location; import android.location.LocationListener; import android.location.Locat
import android.app.Activity;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.widget.Toast;
public class Navigation extends Activity
{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/* Use the LocationManager class to obtain GPS locations */
try
{
LocationManager mlocManager =
(LocationManager) getSystemService(Context.LOCATION_SERVICE);
Toast.makeText(getBaseContext(),
"Provider Enable Status : " + mlocManager.isProviderEnabled(
mlocManager.getProvider("gps").getName()),
Toast.LENGTH_LONG).show();
Location loc = mlocManager.getLastKnownLocation("gps");
Toast.makeText( getApplicationContext(), "" +
loc.getLatitude(), Toast.LENGTH_LONG).show();
Toast.makeText( getApplicationContext(), "" + loc,
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(), e.getMessage() +
e.toString(), Toast.LENGTH_LONG).show();
}
}
}
我已将以下侦听器添加到代码中:
LocationListener mlocListener = new MyLocationListener();
mlocManager.requestLocationUpdates( mlocManager.getProvider("gps").getName(), 0, 0, mlocListener);
在oncreate函数中。下面是一节课
public class MyLocationListener implements LocationListener {
@Override
public void onLocationChanged(Location loc) {
Toast.makeText( getApplicationContext(),
"Hi, location changed. :)", Toast.LENGTH_LONG).show();
try {
loc.getLatitude();
loc.getLongitude();
String Text = "My current location is: " +
" Latitude = " + loc.getLatitude() +
" Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),
Text, Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(getBaseContext(),
e.getMessage(), Toast.LENGTH_LONG).show();
}
}
}
运行窗口后,我将执行以下操作:
在Eclipse中转到DDMS并设置Lattitures/Longtude,然后单击send
但它仍然不会在函数中触发任何toast命令。如果读取,则会明确表示该方法返回“提供程序的最后一个已知位置,或null”
我不是安卓开发者,但我想在GPS冷启动时,它会返回null,直到修复完成。您必须检查loc是否为=继续之前为空。您可以尝试等待一段时间或使用异步回调
希望能有所帮助。这很有道理。。。因为如果设备没有已知的位置,你就找不到。
您应该自己处理空值。提供一个新位置或等待第一个GPS数据…您可以执行空值检查,这绝对是解决此问题的好方法。另一个诀窍是调用活动onResume()来请求位置更新:
@Override
protected void onResume() {
// TODO Auto-generated method stub
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10f,this);
super.onResume();
}
在您的示例中,请确保将其创建为实例变量(类级别),而不是本地变量(未在方法中定义,在您的示例中为onCreate)
这肯定会获取位置更新,而不会抛出空指针。另外,请确保对GPS配置的模拟器进行地理定位。使用DDMS的Emulator控件弹出按钮来完成此操作。在启动“活动”之前,请确定一些经度和纬度值
希望这有帮助 沃伦费思和杰切隆所说的确实是一种好的做法。在使用该对象之前,您应该检查它是否为空
还认为,如果你在你的活动中,就没有必要获得上下文:调用代码> GETApple()/<代码>和<代码> GETBaseEngTeX()/<代码>。将它们替换为
此
,然后重试
您可能还想检查第三个Toast通知,因为您正在将其文本设置为“”+loc
,我猜您的意思是“”+loc.getLongitude()
如果需要将活动的上下文放在另一个类中的某个地方,那么可以这样定义上下文
private Context ctx = null;
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
// some code
ctx = getApplication();
// some more code
}
// your other class in the same Java file (inner class)
public class MyOtherClass {
// a construtctor probably
// some usefull methods
Toast.makeText(ctx, "My message", 5000).show();
}
我添加了LocationListener mlocListener=新的MyLocationListener();mlocManager.requestLocationUpdates(mlocManager.getProvider(“gps”).getName(),0,0,mlocListener);公共类MyLocationListener实现LocationListener{@Override public void onLocationChanged(Location loc){loc.getLatitude();loc.getLatitude();String Text=“Latitud=“+loc.getLatitude()+”Longitud=“+loc.getLatitude();Toast.makeText(getApplicationContext(),Text,Toast.LENGTH_LONG).show();}您将在上下文处理方面遇到严重问题……请看我的答案。代码终于开始工作了。(我将注意您编写的所有良好实践)非常感谢你的建议。我一定会将他们中的每一个都纳入到项目中。因为,我开始不超过2-3天前,我遇到了这样或那样的问题。