我正在尝试使用gps查找位置,但以下代码片段创建问题。错误显示为
Toast类型中的方法makeText(Context,CharSequence,int)不适用
参数(GeocodingMainActivity,String,int)
..........
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location location) {
String format = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
String message = format;
Toast.makeText(GeocodingMainActivity.this, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b)
{Toast.makeText(GeocodingMainActivity.this, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(GeocodingMainActivity.this,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(GeocodingMainActivity.this,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}
答案 0 :(得分:4)
试试这个......
Toast.makeText(getApplicationContext(), "message", Toast.LENGTH_LONG).show();
答案 1 :(得分:1)
试试此代码
Toast.makeText(MyAndroidAppActivity.this,"String!", Toast.LENGTH_LONG).show();
答案 2 :(得分:0)
String address=locationText.getText() + "\n"+addresses.get(0).getAddressLine(0)+", "+
addresses.get(0).getAddressLine(1)+", "+addresses.get(0).getAddressLine(2);
Toast.makeText(getApplicationContext(),address,Toast.LENGTH_LONG).show();
答案 3 :(得分:0)
用下面的代码替换MyLocationListener。并在使用MyLocationListener时传递上下文。
private class MyLocationListener implements LocationListener {
private Activity mContext;
public MyLocationListener(Activity context) {
this.mContext = context;
}
public void onLocationChanged(Location location) {
String format = String.format( "New Location \n Longitude: %1$s \n Latitude: %2$s", location.getLongitude(), location.getLatitude());
String message = format;
Toast.makeText(mContext, message, Toast.LENGTH_LONG).show();
}
public void onStatusChanged(String s, int i, Bundle b)
{
Toast.makeText(mContext, "Provider status changed",Toast.LENGTH_LONG).show();
}
public void onProviderDisabled(String s) {
Toast.makeText(mContext,"Provider disabled by the user. GPS turned off",Toast.LENGTH_LONG).show();
}
public void onProviderEnabled(String s) {
Toast.makeText(mContext,"Provider enabled by the user. GPS turned on",Toast.LENGTH_LONG).show();
}