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

Composants graphiques Android Discussion :

Clic sur un bouton qui ne fonctionne pas


Sujet :

Composants graphiques Android

Vue hybride

Message précédent Message précédent   Message suivant Message suivant
  1. #1
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 61
    Par défaut Clic sur un bouton qui ne fonctionne pas
    Bonjour à tous,

    j'ai un boutton dans un fragment, lors du click sur celui-ci je n'ai aucune réaction. C'est la première foi que j'ai un problème de ce genre... Je ne comprend vraiment pas d'où peut venir mon problème

    Ma classe Java

    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
    public class MyFragmentC extends Fragment{
     
     
    	Button envoyer;
     
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    		getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    		View myFragmentView = inflater.inflate(R.layout.fragment_c, container, false);
    		getActivity().setContentView(R.layout.fragment_c);
     
    		getActivity().setProgressBarIndeterminateVisibility(false);
     
    		envoyer = (Button) getActivity().findViewById(R.id.envoyer);
     
     
    		reactions();
     
    		return myFragmentView;
     
    	}
     
     
     
    	public void reactions(){
     
    		envoyer = (Button)getActivity().findViewById(R.id.envoyer);
     
    		envoyer.setOnClickListener(new View.OnClickListener(){
    		    @Override
    		    public void onClick(View view) {
     
     
    		    	new AlertDialog.Builder(getActivity()).setTitle("Ca marche").show(); 
    		    	Log.d("Etape 1","ok");
     
     
    		    }
     
     
    		});
     
     
    	}
    }



    Mon fichier xml (Le bouton "envoyer" est tout en bas)

    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
    <?xml version="1.0" encoding="utf-8"?>
     
    <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true" 
        android:background="@color/background">
     
     
        <!-- Correspondra au fragment Contact -->
     
     
     
    	<TableLayout android:layout_width="fill_parent"
    		android:layout_height="wrap_content"
    		android:stretchColumns="2">
     
     
     
    		<TextView android:text="@string/vide"/>
     
     
     
     
            <TableRow>
     
                <TextView android:text="@string/vide"/>
     
            </TableRow>
     
     
     
     
            <TableRow>
     
     
                <!-- L'objet de l'avois du message -->
     
                <EditText
                    android:id="@+id/objet"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/textContact1"
                    android:textColor="@color/colorEditText" />
     
     
            	<TextView android:text="@string/vide"/>
     
     
            </TableRow>
     
     
     
     
            <TableRow>
     
     
                <!-- Adresse personnel pour que l'on puisse nous répondre -->
                <EditText
                    android:id="@+id/email"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:inputType="textEmailAddress"
                    android:hint="@string/textContact2" 
                    android:textColor="@color/colorEditText" >
     
                    <requestFocus />
                </EditText>
     
            </TableRow>
     
     
     
            <TableRow>
     
                <TextView android:text="@string/vide"/>
     
            </TableRow>
     
     
     
     
            <TableRow>
     
                <TextView android:text="@string/vide"/>
     
            </TableRow>
     
     
     
     
            <TableRow>
     
     
                <!-- Le message à envoyer -->
     
                <EditText
                    android:id="@+id/message"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:ems="10"
                    android:hint="@string/textContact3"
                    android:inputType="textMultiLine"
                    android:gravity="top|left"
                    android:windowSoftInputMode="adjustPan" 
                    android:textColor="@color/colorEditText" />
     
            	<TextView android:text="@string/vide"/>
     
            </TableRow>
     
     
     
    	<LinearLayout 
    		android:orientation="vertical" 
    		android:layout_width="fill_parent"
    		android:layout_height="fill_parent" 
    		android:gravity="bottom|right"
    		android:layout_marginRight="50dp"
    		android:layout_marginBottom="70dp">
     
     
    	    <!-- Bouton envoyer, pour envoyer le message -->
     
    				<Button
    					android:id="@+id/envoyer"
    					android:layout_width="wrap_content"
    					android:layout_height="wrap_content"
    					android:text="@string/btnContact"
    					android:background="#93DCFE"
    					android:textColor="#ffffff"/>
     
     
    	</LinearLayout>
     
    	</TableLayout>
     
    </ScrollView>

    Pourquoi ne mon bouton ne réagi pas? est-ce parce que je suis dans un Fragment?

    Je vous remercie d'avance si vous avez des éléments de réponse!

    Bien cordialement.

  2. #2
    Membre averti
    Homme Profil pro
    Développeur Java
    Inscrit en
    Février 2013
    Messages
    15
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : France

    Informations professionnelles :
    Activité : Développeur Java

    Informations forums :
    Inscription : Février 2013
    Messages : 15
    Par défaut
    Bonjour,

    je suis débutant en Android, mais j'ai eu le même problème il y'a quelques jours, et quelqu'un disait qu'il fallait modifier getActivity() par le nom de ta class .this

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    public void onClick(View view) {
     
     
    		    	new AlertDialog.Builder(MyFragmentC.this)).setTitle("Ca marche").show(); 
    		    	Log.d("Etape 1","ok");
     
     
    		    }
    j'espère que ça va marcher.

  3. #3
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 61
    Par défaut
    Bonjour,

    merci de ton aide! Mais malheureusement ce n'est pas ça... En fait, le bouton réagi comme si il n'avait pas de Listener (il ne fait rien du tout quoi)

  4. #4
    Membre confirmé
    Profil pro
    Inscrit en
    Mai 2010
    Messages
    61
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mai 2010
    Messages : 61
    Par défaut
    Problème résolu avec thread !

    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
    package com.android.rss;
     
    import java.util.ArrayList;
    import java.util.List;
     
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import org.apache.http.protocol.HTTP;
    import org.apache.http.util.EntityUtils;
     
    import android.app.AlertDialog;
    import android.support.v4.app.Fragment;
    import android.content.Context;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.ViewGroup;
    import android.view.WindowManager;
    import android.view.inputmethod.InputMethodManager;
    import android.widget.Button;
     
    public class MyFragmentC extends Fragment{
     
     
    	Button envoyer;
     
    	@Override
    	public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    		getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
    		setHasOptionsMenu(true);
     
     
    		//Initialisation du fragmentView
    		View myFragmentView = LayoutInflater.from(getActivity().getBaseContext()).inflate(R.layout.fragment_c, container, false);
     
    		reactions();	  
     
     
    		return myFragmentView;
     
    	}
     
     
    	@Override
    	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
     
    	    inflater.inflate(R.menu.main, menu);
     
    	    menu.getItem(0).setVisible(false);
     
     
    	}	
     
    	public void reactions(){
     
     
    		new Thread (new Runnable(){
     
    			public void run(){
    				envoyer = (Button)getView().findViewById(R.id.send);
     
    				envoyer.setOnClickListener(new View.OnClickListener(){
    				    @Override
    				    public void onClick(View view) {
     
    				    	try {
     
    					        HttpClient client = new DefaultHttpClient();  
    					        String postURL = "http://www.google.fr/";
    					        HttpPost post = new HttpPost(postURL); 
    					        List<NameValuePair> params = new ArrayList<NameValuePair>();
    					        params.add(new BasicNameValuePair("objet", getActivity().findViewById(R.id.objet).toString()));
    					        getActivity().findViewById(R.id.objet).setTag("");
    					        params.add(new BasicNameValuePair("email", getActivity().findViewById(R.id.email).toString()));
    					        params.add(new BasicNameValuePair("message", getActivity().findViewById(R.id.message).toString()));
    					        UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8);
    					        post.setEntity(ent);
    					        HttpResponse responsePOST = client.execute(post);  
    					        HttpEntity resEntity = responsePOST.getEntity();  
    					        if (resEntity != null) {    
    					            Log.d("RESPONSE",EntityUtils.toString(resEntity));
    					        }else{Log.d("NO REP","Pas de réponse");}
    					        Log.d("Etape 1","ok");
    					    } catch (Exception e) {
    					        e.printStackTrace();
    					        Log.d("NO REP","Pas de réponse");
    					    }
     
     
     
    				    }
     
     
    				});
    			}
    		}).start();
     
     
    	}
     
     
     
     
    }

    Le code pour ceux qui ont le même problème!

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. Probleme sur une requete qui ne fonctionne pas
    Par tralala2 dans le forum Langage SQL
    Réponses: 2
    Dernier message: 02/08/2009, 10h05
  2. Lien sur un bouton qui ne veut pas fonctionner :/
    Par Rayono dans le forum Flash
    Réponses: 0
    Dernier message: 12/05/2009, 11h14
  3. Bouton qui ne fonctionne pas
    Par presser dans le forum Excel
    Réponses: 7
    Dernier message: 20/02/2009, 09h57
  4. concat sur un string qui ne fonctionne pas
    Par zenux dans le forum Langage
    Réponses: 3
    Dernier message: 28/03/2008, 11h57
  5. [MySQL] Code PHP sur des UPDATE qui ne fonctionne pas
    Par bilou95 dans le forum PHP & Base de données
    Réponses: 2
    Dernier message: 08/10/2007, 11h57

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