2 votes

Toast.makeText() affichant une erreur

J'essaie de trouver un emplacement en utilisant le GPS mais le code suivant du snippet crée un problème. L'erreur s'affiche comme suit

La méthode makeText(Context, CharSequence, int) dans le type Toast n'est pas applicable.
pour les 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();
 }

5voto

Deepzz Points 2890

Essayez ceci...

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

1voto

Angel Points 41

Essayez ce code

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

0voto

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

0voto

Android Team Points 7530

Remplacez votre MyLocationListener par le code ci-dessous. et passez le contexte lorsque vous utilisez votre 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();
 }

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X