Tayyaba Jan
Tayyaba Jan

Reputation: 21

Toast.makeText() displaying error

i am trying to find location using gps but the following code of snippet creating problem. The error is displaying as

The method makeText(Context, CharSequence, int) in the type Toast is not applicable
for the arguments (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();
 }

Upvotes: 2

Views: 6500

Answers (4)

user4571931
user4571931

Reputation:

Replace your MyLocationListener with below code. and pass context when you use your 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();
 }

Upvotes: 0

jignesh k thanki
jignesh k thanki

Reputation: 71

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();

Upvotes: 0

Deepzz
Deepzz

Reputation: 4571

Try this...

Toast.makeText(getApplicationContext(), "message", Toast.LENGTH_LONG).show();

Upvotes: 6

Angel
Angel

Reputation: 153

Try this code

     Toast.makeText(MyAndroidAppActivity.this,"String!", Toast.LENGTH_LONG).show();

Upvotes: 1

Related Questions