IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Java Discussion :

Erreur "non-static variable this cannot be referenced from a static context"


Sujet :

Java

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2010
    Messages
    76
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2010
    Messages : 76
    Par défaut Erreur "non-static variable this cannot be referenced from a static context"
    Bonjour,

    Je travaille sur un projet WinDev Mobile 18 et je cherche à faire fonctionner un exemple d'envoi automatique d'emails.

    J'ai trouvé sur internet l'exemple : http://www.jondev.net/articles/Sendi...%29_in_Android

    Je tourne en rond avec des erreurs Java...

    Si je déclare la fonction snd avec static j'ai l'erreur :
    non-static variable this cannot be referenced from a static context
    Sinon l'objet m de la classe Mail est inconnu message :
    Cannot find symbol m.setTo(toArr);
    Voilà la liste des erreurs que j'ai :
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:211: error: non-static variable this cannot be referenced from a static context
    Mail m = new Mail("erreurlog@gmail.com", "XXXX");
    ^
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:215: error: cannot find symbol
    m.setTo(toArr);
    ^
    symbol: method setTo(String[])
    location: variable m of type GWDCPProceduresGlobales.Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:217: error: cannot find symbol
    m.setFrom("erreurlog@gmail.com");
    ^
    symbol: method setFrom(String)
    location: variable m of type GWDCPProceduresGlobales.Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:219: error: cannot find symbol
    m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device.");
    ^
    symbol: method setSubject(String)
    location: variable m of type GWDCPProceduresGlobales.Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:230: error: cannot find symbol
    Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show();
    ...
    Quelqu'un saurait-il m'indiquer comment résoudre ces problèmes ?

    Merci d'avance pour votre aide.

  2. #2
    Membre extrêmement actif
    Avatar de MarieKisSlaJoue
    Homme Profil pro
    Ingénieur Cloud
    Inscrit en
    Mai 2012
    Messages
    1 145
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Roumanie

    Informations professionnelles :
    Activité : Ingénieur Cloud
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Mai 2012
    Messages : 1 145
    Billets dans le blog
    20
    Par défaut
    Bonjour, cette erreur interviens quand dans une fonction static tu utilises des variable qui ne sont pas static mais qui appartienne à une objet instancier.
    Pour régler ça. Soit tu passe tes variable utile en statique si c'est possible. Soit tu n'utilise pas de fonction static mais des fonction membre.

    Ton code est certainement mal conçu/découpé pour avoir ce genre d'erreur.
    Ce post à été écrit par un panda
    Apollo 11 - AGC revue de code
    -- qwerty keybord

  3. #3
    Membre Expert
    Inscrit en
    Mai 2006
    Messages
    1 364
    Détails du profil
    Informations forums :
    Inscription : Mai 2006
    Messages : 1 364
    Par défaut
    Il n'y a pas de fonction snd dans la page que tu as posté. Apres avoir jeté un oeil, le code qui englobe setTo sur le lien a l'air correct. Donc je dirais que c'est le code que tu as fait qui ne fonctionne pas. Peux tu le poster qu'on sache de quoi on parle ?

  4. #4
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2010
    Messages
    76
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2010
    Messages : 76
    Par défaut
    Bonjour,

    Merci de vos réponse

    Je vous informe que ce n'est pas mon code

    Je l'avais trouvé sur internet d’après les utilisateurs ce code ça marche bien sur IDE de java Eclipse ou Androïde studio

    Moi je veux l'adapter pour marcher avec Windev Mobile 18 !

    en windev mobile si on veux utiliser un code java on le met dans une procédure globale

    donc ce que j'ai fais moi : création de deux procédure globales

    1- contient la déclaration et la classe Mail


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    import java.util.Date; 
    import java.util.Properties; 
    import javax.activation.CommandMap; 
    import javax.activation.DataHandler; 
    import javax.activation.DataSource; 
    import javax.activation.FileDataSource; 
    import javax.activation.MailcapCommandMap; 
    import javax.mail.BodyPart; 
    import javax.mail.Multipart; 
    import javax.mail.PasswordAuthentication; 
    import javax.mail.Session; 
    import javax.mail.Transport; 
    import javax.mail.internet.InternetAddress; 
    import javax.mail.internet.MimeBodyPart; 
    import javax.mail.internet.MimeMessage; 
    import javax.mail.internet.MimeMultipart; 
    import javax.mail.*; 
     
     
    public static void Déclarations(){
    }
     
     
    public  static class Mail extends javax.mail.Authenticator { 
     
    	private String _user; 
    	private String _pass; 
     
    	private String[] _to; 
    	private String _from; 
     
    	private String _port; 
    	private String _sport; 
     
    	private String _host; 
     
    	private String _subject; 
    	private String _body; 
     
    	private boolean _auth; 
     
    	private boolean _debuggable; 
     
    	private Multipart _multipart; 
     
     
    	public Mail() { 
    		_host = "smtp.gmail.com"; // default smtp server 
    		_port = "465"; // default smtp port 
    		_sport = "465"; // default socketfactory port 
     
    		_user = ""; // username 
    		_pass = ""; // password 
    		_from = ""; // email sent from 
    		_subject = ""; // email subject 
    		_body = ""; // email body 
     
    		_debuggable = false; // debug mode on or off - default off 
    		_auth = true; // smtp authentication - default on 
     
    		_multipart = new MimeMultipart(); 
     
    		// There is something wrong with MailCap, javamail can not find a handler for the multipart/mixed part, so this bit needs to be added. 
    		MailcapCommandMap mc = (MailcapCommandMap) CommandMap.getDefaultCommandMap(); 
    		mc.addMailcap("text/html;; x-java-content-handler=com.sun.mail.handlers.text_html"); 
    		mc.addMailcap("text/xml;; x-java-content-handler=com.sun.mail.handlers.text_xml"); 
    		mc.addMailcap("text/plain;; x-java-content-handler=com.sun.mail.handlers.text_plain"); 
    		mc.addMailcap("multipart/*;; x-java-content-handler=com.sun.mail.handlers.multipart_mixed"); 
    		mc.addMailcap("message/rfc822;; x-java-content-handler=com.sun.mail.handlers.message_rfc822"); 
    		CommandMap.setDefaultCommandMap(mc); 
    	} 
     
    	public Mail(String user, String pass) { 
    		this(); 
     
    		_user = user; 
    		_pass = pass; 
    	} 
     
    	public boolean send() throws Exception { 
    		Properties props = _setProperties(); 
     
    		if(!_user.equals("") && !_pass.equals("") && _to.length > 0 && !_from.equals("") && !_subject.equals("") && !_body.equals("")) { 
    			Session session = Session.getInstance(props, this); 
     
    			MimeMessage msg = new MimeMessage(session); 
     
    			msg.setFrom(new InternetAddress(_from)); 
     
    			InternetAddress[] addressTo = new InternetAddress[_to.length]; 
    			for (int i = 0; i < _to.length; i++) { 
    				addressTo[i] = new InternetAddress(_to[i]); 
    			} 
    			msg.setRecipients(MimeMessage.RecipientType.TO, addressTo); 
     
    			msg.setSubject(_subject); 
    			msg.setSentDate(new Date()); 
     
    			// setup message body 
    			BodyPart messageBodyPart = new MimeBodyPart(); 
    			messageBodyPart.setText(_body); 
    			_multipart.addBodyPart(messageBodyPart); 
     
    			// Put parts in message 
    			msg.setContent(_multipart); 
     
    			// send email 
    			Transport.send(msg); 
     
    			return true; 
    		} else { 
    			return false; 
    		} 
    	} 
     
    	public void addAttachment(String filename) throws Exception { 
    		BodyPart messageBodyPart = new MimeBodyPart(); 
    		DataSource source = new FileDataSource(filename); 
    		messageBodyPart.setDataHandler(new DataHandler(source)); 
    		messageBodyPart.setFileName(filename); 
     
    		_multipart.addBodyPart(messageBodyPart); 
    	} 
     
    	//@Override 
    	public PasswordAuthentication getPasswordAuthentication() { 
    		return new PasswordAuthentication(_user, _pass); 
    	} 
     
    	private Properties _setProperties() { 
    		Properties props = new Properties(); 
     
    		props.put("mail.smtp.host", _host); 
     
    		if(_debuggable) { 
    			props.put("mail.debug", "true"); 
    		} 
     
    		if(_auth) { 
    			props.put("mail.smtp.auth", "true"); 
    		} 
     
    		props.put("mail.smtp.port", _port); 
    		props.put("mail.smtp.socketFactory.port", _sport); 
    		props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); 
    		props.put("mail.smtp.socketFactory.fallback", "false"); 
     
    		return props; 
    	} 
     
    	// the getters and setters 
    	public String getBody() { 
    		return _body; 
    	} 
     
    	public void setBody(String _body) { 
    		this._body = _body; 
    	} 
     
    	// more of the getters and setters ….. 
    }

    puis la 2 procédure globale :
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    public static void SendEmail()
     
    {
     
    Mail m = new Mail("gmailusername@gmail.com", "password"); 
     
    String[] toArr = {"bla@bla.com", "lala@lala.com"}; 
    m.setTo(toArr); 
    m.setFrom("wooo@wooo.com"); 
    m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
    m.setBody("Email body."); 
     
    try { 
    	m.addAttachment("/sdcard/filelocation"); 
     
    	if(m.send()) { 
    		Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
    	} else { 
    		Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
    	} 
    } catch(Exception e) { 
    	//Toast.makeText(MailApp.this, "There was a problem sending the email.", Toast.LENGTH_LONG).show(); 
    	Log.e("MailApp", "Could not send email", e); 
    } 
    }

    Quand on veux déployer APK du mon projet avec Windev mobile j'ajoute les rois bibliotéhques JAR : activation.JAR, additionnal.JAR et mail.JAR


    J'avais ce message d'erreur :

    Echec de la création de l'application Android <C:\Mes Projets Mobile\SendMail\Exe\SendMail.apk>.


    Ligne de commande : "C:\Program Files\Java\jdk1.8.0_25\bin\javac.exe" -encoding UTF-16LE -nowarn -source 1.5 -target 1.5 -d bin\classes -bootclasspath "C:\Android\android-sdk\platforms\android-22\android.jar" gen\com\masociete\sendmail\*.java src\com\masociete\sendmail\wdgen\*.java -classpath "libs;libs\activation.jar;libs\additionnal.jar;libs\mail.jar"


    Erreur retournée :
    Code console : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:196: error: cannot find symbol
    m.setTo(toArr); 
     ^
      symbol:   method setTo(String[])
      location: variable m of type Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:197: error: cannot find symbol
    m.setFrom("wooo@wooo.com"); 
     ^
      symbol:   method setFrom(String)
      location: variable m of type Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:198: error: cannot find symbol
    m.setSubject("This is an email sent using my Mail JavaMail wrapper from an Android device."); 
     ^
      symbol:   method setSubject(String)
      location: variable m of type Mail
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:205: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
    		               ^
      symbol:   class MailApp
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:205: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
    		                                                             ^
      symbol:   variable Toast
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:205: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was sent successfully.", Toast.LENGTH_LONG).show(); 
    		^
      symbol:   variable Toast
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:207: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
    		               ^
      symbol:   class MailApp
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:207: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
    		                                                    ^
      symbol:   variable Toast
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:207: error: cannot find symbol
    		Toast.makeText(MailApp.this, "Email was not sent.", Toast.LENGTH_LONG).show(); 
    		^
      symbol:   variable Toast
      location: class GWDCPProceduresGlobales
    src\com\masociete\sendmail\wdgen\GWDCPProceduresGlobales.java:211: error: cannot find symbol
    	Log.e("MailApp", "Could not send email", e); 
    	^
      symbol:   variable Log
      location: class GWDCPProceduresGlobales
    10 errors

    ça fais une semaine que je tourne en rond

  5. #5
    Membre expérimenté
    Homme Profil pro
    Inscrit en
    Juillet 2012
    Messages
    200
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Bénin

    Informations forums :
    Inscription : Juillet 2012
    Messages : 200
    Par défaut
    Bonsoir. Il me semble que l'erreur indique que les fonctions setTo, setSubject, etc. ne sont pas définies... Ce qui est normal puisque je ne trouve leur définition nulle part dans le code de la classe Mail.

  6. #6
    Membre confirmé
    Profil pro
    Développeur informatique
    Inscrit en
    Novembre 2010
    Messages
    76
    Détails du profil
    Informations personnelles :
    Localisation : Maroc

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Novembre 2010
    Messages : 76
    Par défaut
    Citation Envoyé par esired Voir le message
    Bonsoir. Il me semble que l'erreur indique que les fonctions setTo, setSubject, etc. ne sont pas définies... Ce qui est normal puisque je ne trouve leur définition nulle part dans le code de la classe Mail.
    Bonjour,

    Normalement quand je déploies L'APK j'ajoute les bibliothèques JAR , comment je peux ajouter ces bibliothèques sur le code JAVA stp directement ?

    Merci de votre réponse

Discussions similaires

  1. Non-static variable cannot be referenced from a static context
    Par otylio dans le forum Débuter avec Java
    Réponses: 4
    Dernier message: 23/11/2018, 15h42
  2. Réponses: 3
    Dernier message: 23/07/2015, 14h59
  3. Réponses: 4
    Dernier message: 12/02/2015, 10h27
  4. non-static method cannot be referenced from a static context
    Par Spawny123 dans le forum Débuter avec Java
    Réponses: 3
    Dernier message: 04/03/2010, 21h38
  5. [FOP] [ERROR] non-static variable this cannot be referenced from a static context
    Par cash3000 dans le forum API standards et tierces
    Réponses: 8
    Dernier message: 05/05/2006, 16h46

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo