我正在创建一个应用程序,要求在继续之前找到用户的GPS位置。我这样做是通过实现LocationListener接口。
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if(loc!=null){
loc.getLatitude();
loc.getLongitude();
text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
Toast.makeText(getApplicationContext(),text, Toast.LENGTH_SHORT).show();
}
}
但是我在getApplicationContext()方法中遇到了NullPointerException。我也试过MyClass.this,但结果相同。当我在onCreate中显示它时,toast工作正常。我查找了与此类似的其他问题,但没有一个解决方案有效。我在onCreate中使用了setContentView,如下所示:
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gpslocation);
}
logcat显示:
03-14 14:05:48.396: E/AndroidRuntime(604): FATAL EXCEPTION: main
03-14 14:05:48.396: E/AndroidRuntime(604): java.lang.NullPointerException
03-14 14:05:48.396: E/AndroidRuntime(604): at emergency.app.MyLocationListener.onLocationChanged(MyLocationListener.java:53)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.location.LocationManager$ListenerTransport._handleMessage(LocationManager.java:227)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.location.LocationManager$ListenerTransport.access$000(LocationManager.java:160)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.location.LocationManager$ListenerTransport$1.handleMessage(LocationManager.java:176)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:137)
03-14 14:05:48.396: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:4340)
03-14 14:05:48.396: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 14:05:48.396: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:511)
03-14 14:05:48.396: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-14 14:05:48.396: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-14 14:05:48.396: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method)
有人能告诉我如何克服这个问题吗?我是android的初学者,所以我真的需要帮助!
答案 0 :(得分:0)
使用当前上下文(= this)而不是应用程序范围的上下文。
Toast.makeText(this,text, Toast.LENGTH_SHORT).show();
答案 1 :(得分:0)
尝试以下代码
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
Toast.makeText(YourActivity.this, (String) msg.obj,
Toast.LENGTH_SHORT).show();
}
};
};
public void onLocationChanged(Location loc) {
// TODO Auto-generated method stub
if(loc!=null){
loc.getLatitude();
loc.getLongitude();
text = "My current location is: " + "Latitude = " + loc.getLatitude() + "Longitude = " + loc.getLongitude();
Message msg = new Message();
msg.what=0;
msg.obj= text;
handler.sendMessage(msg);
}
}
答案 2 :(得分:0)
下面的代码在我的手机中工作,并在Toast中给我定位点。
public class LocationActivity extends Activity implements LocationListener {
Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (msg.what == 0) {
Toast.makeText(LocationActivity.this, (String) msg.obj,
Toast.LENGTH_SHORT).show();
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LocationManager locationManager = (LocationManager) this
.getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
@Override
public void onLocationChanged(Location location) {
System.out.println("onLocationChanged :");
if (location != null) {
location.getLatitude();
location.getLongitude();
String text = "My current location is: " + "Latitude = "
+ location.getLatitude() + "Longitude = "
+ location.getLongitude();
Message msg = new Message();
msg.what = 0;
msg.obj = text;
handler.sendMessage(msg);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
}
还在清单文件中添加权限。
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />