show() n'affiche pas de message
Bonjour,
Je suis en train de faire une appli pour m'exercer.
C'est un chronomètre qui se compose de deux pages :
la page mère (MainActivity.java).
la page fille (MonActivite2.java).
Depuis la mère , lorsque l'on clique sur le bouton start, un chrono se lance et s'ouvre la page fille qui demande votre nom (je sais, c'est complètement débile mais c'est pas grave ... c'est pour s'entraîner à ouvrir une autre page puis a en récupérer et afficher une donnée transmise de la page fille à la mère !).
Ensuite, un bouton "valider" nour renvoie vers la page mère en y renvoyant les données pour affichage via la fonction show() qui se trouve à la ligne 84 de la page mère nommée "MainActivity.java".
mon problème est que rien ne s'affiche lorsque je reviens sur la page mère.
donc voici mon code page mère : MainActivity.java (avec la fonction show() en ligne 84)
Code:
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
| package pack.andro_01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.SystemClock;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Toast;
public class MainActivity extends Activity implements OnClickListener{
//Déclaration des instances des widgets de l'écran "main"
Button monBoutonStart;
Button monBoutonPause;
Button monBoutonReset;
Chronometer monChrono;
/** called when the activity is first created**/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Affichage de l'écran de démarrage
setContentView(R.layout.activity_main);
//Xréation des instances du bouton et du chrono de l'écran "main"
monBoutonStart = (Button)findViewById(R.id.monBoutonStartId);
monBoutonPause = (Button)findViewById(R.id.monBoutonPauseId);
monBoutonReset = (Button)findViewById(R.id.monBoutonResetId);
monChrono = (Chronometer)findViewById(R.id.monChronoId);
//Ecouteur du bouton
monBoutonStart.setOnClickListener(listenStart);
monBoutonPause.setOnClickListener(listenStop);
monBoutonReset.setOnClickListener(listenReset);
}
private OnClickListener listenStart = new OnClickListener() {
@Override
public void onClick(View v) {
// Arrêt du chrono
monChrono.start();
//Instanciation de l'objet "monIntent" utilisé pour communiquer avec la deuxième activité
Intent monIntent = new Intent(MainActivity.this, MonActivite2.class);
startActivityForResult(monIntent, 0);
}
};
private OnClickListener listenStop = new OnClickListener() {
@Override
public void onClick(View v) {
// Arrêt du chrono
monChrono.stop();
}
};
private OnClickListener listenReset = new OnClickListener() {
@Override
public void onClick(View v) {
// Arrêt du chrono
monChrono.setBase(SystemClock.elapsedRealtime());
monChrono.stop();
}
};
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
public void OnActivityResult(int requestCode, int resultCode, Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
//On récupère le message identifié par le paramètre "nom" de l'intent
String nom = data.getStringExtra("nom");
//On affiche le message
Toast.makeText(this, "Votre nom est : " + nom, Toast.LENGTH_LONG).show();
}
} |
et voici mon code page fille : MonActivite2.java
Code:
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
| package pack.andro_01;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MonActivite2 extends Activity implements OnClickListener{
//Déclaration de l'instance du bouton "valider" de l'écran d'édition
Button monBtnValider;
//Déclaration d'un objet de type "EditText"
EditText editTextNom;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//Affichage de l'écr(an d'édition
setContentView(R.layout.ecran_edition);
//Instanciation de l'objet "monBtnValider" avec le widget de l'écran
monBtnValider = (Button)findViewById(R.id.btnValiderId);
//Instanciation de l'objet "editTextNom" avec le widget de l'écran
editTextNom = (EditText)findViewById(R.id.editTextNomId);
//Ecouteur du bouton
monBtnValider.setOnClickListener(this);
}
@Override
public void onClick(View v) {
//traitement après un click sur "valider"
//On vérifie que la taille de la chaîne de retouir est supérieure à 0
if(editTextNom.getText().length() > 0)
{
//Instanciation de l'objet "monIntent" utilisé pour communiquer
//avec la première activité
Intent monIntent2 = new Intent();
//On ajoute le nom saisi dans l'Intent
monIntent2.putExtra("nom", editTextNom.getText().toString());
//on retourne le résultat avec l'intent
setResult(RESULT_OK, monIntent2);
//On ferme cette activité
finish();
}
}
} |
le xml pour MainActivity : "activity_main.xml
Code:
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
| <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:text="Start chrono"
android:id="@+id/monBoutonStartId"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Pause chrono"
android:id="@+id/monBoutonPauseId"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Reset chrono"
android:id="@+id/monBoutonResetId"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Chronometer
android:text="Chronometer"
android:id="@+id/monChronoId"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
</Chronometer>
</LinearLayout> |
le xml pour MonActivite2 : "ecranEdition.xml
Code:
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
| <?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:text="coucou ! ma deuxième activité"
android:layout_width="match_parent">
</TextView>
<TextView
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:text="Quel est votre nom ?"
android:layout_width="match_parent">
</TextView>
<EditText
android:layout_height="wrap_content"
android:id="@+id/editTextNomId"
android:layout_width="match_parent">
<requestFocus></requestFocus>
</EditText>
<Button
android:id="@+id/btnValiderId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout> |
Merci à vous de bien vouloir m'aider !