2013-05-12 2 views
0

Quand je clique dans mon bouton avec l'identifiant "faouzi", ça ne marche pas! Est-ce que quelqu'un peut m'aider? Mon objectif est de montrer une activité nommée ChangePassword mais il ne lance pas cette activité & ne rien faire ..mon bouton ne fait rien (lancer une nouvelle activité en cliquant sur mon bouton)

public class MainActivity extends Activity { 

    SharedPreferences Pref; 

    SharedPreferences prefs; 
    SharedPreferences preferences; 
    SharedPreferences.Editor editor; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     Pref = getSharedPreferences("myPassword",0); 
     String password = Pref.getString("passwordRetrieve", "8888"); 

     super.onCreate(savedInstanceState); 
     //Supprimer la barre de titre de l'activité 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     setContentView(R.layout.activity_main); 

     //Je change la couleur de fond du bouton en bleu 
     Button button = (Button)findViewById(R.id.connection); 
     //button.getBackground().setColorFilter(Color.BLUE, PorterDuff.Mode.MULTIPLY); 
     button.setText(password); 

     //rendre le bouton "Connection" fonctionnel 
     button.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) 
      { 
       EditText text = (EditText)findViewById(R.id.passwordedit); 
       String str = text.getText().toString(); 

       if(str.equals("1234")) 
       { 
        //on va vérifier si l'application a déjà été lancée, si c'est le cas on saute certaines étapes d'initialisat° 
        preferences = MainActivity.this.getSharedPreferences("nbRepet", MODE_PRIVATE);  
        int value = preferences.getInt("nbRepet", 0); 
        if(value<1) 
        { 
         sendConnection(v); 
        } 
        else 
        { 
         Intent intent = new Intent(MainActivity.this, RegulationTime.class); 
         startActivity(intent); 
        } 
       } 
       else 
       { 
        AlertDialog alertdialog = new AlertDialog.Builder(MainActivity.this).create(); 
        alertdialog.setTitle("Wrong Password"); 
        alertdialog.setIcon(R.drawable.wrongpassword); 
        alertdialog.setMessage("The password you entered is incorrect, please try again!"); 
        alertdialog.show(); 
       } 
      } 
     }); 

     //Lorsqu'on clique sur le bouton Change Password 
     Button changepassword = (Button)findViewById(R.id.faouzi); 
     changepassword.setOnClickListener(new View.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       Intent intent = new Intent(MainActivity.this, ChangePassword.class); 
       startActivity(intent); 
      } 
     }); 
    } 


    //Fonction appelée pour lancer l'activité "AddMasterNumber" 
    public void sendConnection(View v) 
    { 
     Intent intent = new Intent(this, AddRecipientNumber.class); 
     startActivity(intent); 

//La nouvelle valeur de "nbRepet" devient 1 & nous empêche de relancer la série d'activités une 2nde fois 
//L'utilisateur ne doit procéder à certaines étapes 1seule fois (ex: entrer le n° de la socket); on évite cela! 
     prefs = getSharedPreferences("nbRepet",Context.MODE_PRIVATE); 
     editor = prefs.edit(); 
     editor.putInt("nbRepet", 1); 
     editor.commit(); 
    } 

} 

et le fichier XML suivant pour la mise en page:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context=".MainActivity" > 

    <TextView 
     android:id="@+id/text1" 
     android:layout_marginTop="20dp" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="20sp" 
     android:layout_alignLeft="@+id/passwordedit" 
     android:text="@string/connection" /> 

    <TextView 
     android:id="@+id/text2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/passwordedit" 
     android:layout_below="@+id/text1" 
     android:text="@string/password" 
     android:layout_marginTop="30dp" /> 

    <EditText 
     android:layout_height="wrap_content" 
     android:layout_width="250dp" 
     android:layout_centerHorizontal="true" 
     android:layout_below="@+id/text2" 
     android:id="@+id/passwordedit" 
     android:inputType="numberPassword" /> 

    <Button 
     android:id="@+id/connection" 
     android:layout_height="wrap_content" 
     android:layout_width="250dp" 
     android:layout_below="@+id/passwordedit" 
     android:layout_marginTop="10dp" 
     android:layout_centerHorizontal="true" 
     android:onClick="sendConnection" 
     android:text="@string/connection" /> 

    <TextView 
     android:id="@+id/defaultpassword" 
     android:layout_alignLeft="@+id/passwordedit" 
     android:layout_width="fill_parent" 
     android:layout_marginTop="30dp" 
     android:layout_height="wrap_content" 
     android:text="@string/defaultpassword" 
     android:layout_below="@+id/connection" /> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:text="@string/change" 
     android:layout_below="@+id/defaultpassword" 
     android:id="@+id/change" /> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:onClick="onClick" 
     android:layout_below="@+id/change" 
     android:id="@+id/faouzi" 
     /> 
</RelativeLayout> 

et ici fichier AndroidManifest :

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.automatik" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="7" 
     android:targetSdkVersion="17" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.automatik.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity 
      android:name="com.example.automatik.AddMasterNumber" 
      android:label="@string/title_activity_add_master_number" 
      android:parentActivityName="com.example.automatik.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.automatik.MainActivity" /> 
     </activity> 
     <activity 
      android:name="com.example.automatik.AddRecipientNumber" 
      android:label="@string/title_activity_add_recipient_number" 
      android:parentActivityName="com.example.automatik.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.automatik.MainActivity" /> 
     </activity> 
     <activity 
      android:name="com.example.automatik.RegulationTime" 
      android:label="@string/title_activity_regulation_time" 
      android:parentActivityName="com.example.automatik.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.automatik.MainActivity" /> 
     </activity> 
     <activity 
      android:name="com.example.automatik.ChangePassword" 
      android:label="@string/title_activity_change_password" 
      android:parentActivityName="com.example.automatik.MainActivity" > 
      <meta-data 
       android:name="android.support.PARENT_ACTIVITY" 
       android:value="com.example.automatik.MainActivity" /> 
     </activity> 
    </application> 

</manifest> 
+0

Votre activité ChangePassword est-elle déclarée dans AndroidManifest? – GVillani82

+0

@ Joseph82 Oui mon ami, je vais éditer ma question et ajouter le fichier AndroidManifest;) – d3vpasha

Répondre

2

Supprimer cette ligne à partir du bouton faouzi dans votre xml:

Vous déclarez que le bouton a un onClick et utilisez cette méthode publique dans votre fichier XML ou vous définissez l'onClickListener, pas les deux!

+0

je le fais mais ça ne change rien! – d3vpasha

+0

Il est étrange ... le code semble ok – GVillani82

+0

@ Joseph82 je sais, j'ai quelques problèmes aujourd'hui avec éclipse, je ne sais pas pourquoi certaines choses ne fonctionnent pas: s – d3vpasha

Questions connexes