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 :

TabbedActivity problème dès utilisation d'un spinner et d'un DatePickerDialog


Sujet :

Composants graphiques Android

  1. #1
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut TabbedActivity problème dès utilisation d'un spinner et d'un DatePickerDialog
    Bonjour,

    Pourquoi dès l'utilisation du spinner et d'un DatePickerDialog., ça plante et m'indique cette erreur dans le logcat:

    06-14 10:46:44.253: ERROR/AndroidRuntime(2994): android.view.WindowManager$BadTokenException: Unable to add window -- token android.app.LocalActivityManager$LocalActivityRecord@40532780 is not valid; is your activity running?


    Merci.

  2. #2
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    Pourrais tu nous montrer un peu de ton code , surtout les parties ou tu modifies l'UI .

    Merci
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  3. #3
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    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
     
    public class MaClasse extends TabbedActivity {
     
     
    	private TextView mDateDisplay;
    	private Button mPickDate;
    	private int mYear;
    	private int mMonth;
    	private int mDay;
    	private Spinner spinner;
    	private String array_spinner[];
    	static final int DATE_DIALOG_ID = 0;
    	protected ProgressDialog progressDialog = null;
     
    	public void onCreate(Bundle savedInstanceState) {
    		super.onCreate(savedInstanceState);
    		setContentView(R.layout.monlayout);
     
    		// capture our View elements
    		mDateDisplay = (TextView) this.findViewById(R.id.dateDisplay);
    		mPickDate = (Button) this.findViewById(R.id.pickDate);
    		spinner = (Spinner) this.findViewById(R.id.spinner);
    		// TODO call DataBase to find all destinations
     
    		array_spinner = new String[2];
    		array_spinner[0] = "Bonjour";
    		array_spinner[1] = "Au revoir";
     
     
    		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    				android.R.layout.simple_spinner_dropdown_item, array_spinner);
     
    		spinner.setAdapter(adapter);
    		spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
    			public void onItemSelected(AdapterView<?> arg0, View arg1,
    					int arg2, long arg3) {
    				int index = spinner.getSelectedItemPosition();
    				Toast.makeText(getBaseContext(),
    						"You have selected item : " + array_spinner[index],
    						Toast.LENGTH_SHORT).show();
    			}
     
    			public void onNothingSelected(AdapterView<?> arg0) {
    			}
     
    		});
     
    		// add a click listener to the button
    		mPickDate.setOnClickListener(new View.OnClickListener() {
    			public void onClick(View v) {
    				showDialog(DATE_DIALOG_ID);
    			}
    		});
     
    		// get the current date
    		final Calendar c = Calendar.getInstance();
    		mYear = c.get(Calendar.YEAR);
    		mMonth = c.get(Calendar.MONTH);
    		mDay = c.get(Calendar.DAY_OF_MONTH);
    		// display the current date (this method is below)
    		updateDisplay();
     
    	}
     
    	// updates the date in the TextView
    	private void updateDisplay() {
    		mDateDisplay.setText(new StringBuilder()
    				// Month is 0 based so add 1
    				.append(mMonth + 1).append("-").append(mDay).append("-")
    				.append(mYear).append(" "));
    		EditText champdate = (EditText) this.findViewById(R.id.date);
    		champdate.setText(mDay + "/" + (mMonth + 1) + "/" + mYear);
    	}
     
    	// the callback received when the user "sets" the date in the dialog
    	private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
    		// date update
    		public void onDateSet(DatePicker view, int year, int monthOfYear,
    				int dayOfMonth) {
    			mYear = year;
    			mMonth = monthOfYear;
    			mDay = dayOfMonth;
    			updateDisplay();
    		}
    	};
     
    	@Override
    	protected Dialog onCreateDialog(int id) {
    		switch (id) {
    		case DATE_DIALOG_ID:
    			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);
    		}
    		return null;
    	}
    }

    Mon layout:

    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
     
    <?xml version="1.0" encoding="utf-8"?>
    <TabWidget xmlns:android="http://schemas.android.com/apk/res/android"
    	android:layout_width="fill_parent" android:layout_height="fill_parent"
    	android:id="@android:id/tabhost">
    	<LinearLayout android:layout_width="fill_parent"
    		android:layout_height="fill_parent" android:orientation="vertical">
     
    		<Spinner android:id="@+id/spinner" android:layout_width="match_parent"
    			android:layout_height="wrap_content" />
     
    		<TextView android:id="@+id/date"
    			android:layout_width="wrap_content" android:layout_height="wrap_content"
    			android:text="Date de départ " />
     
     
    		<TextView android:id="@+id/dateDisplay" android:layout_width="wrap_content"
    			android:layout_height="wrap_content" android:text="" />
    		<Button android:id="@+id/pickDate" android:layout_width="wrap_content"
    			android:layout_height="wrap_content" android:text="Change the date" />
    	</LinearLayout>
    </TabWidget>

  4. #4
    Inscrit

    Profil pro
    Inscrit en
    Février 2008
    Messages
    658
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Février 2008
    Messages : 658
    Points : 892
    Points
    892
    Par défaut
    Cette erreur apparait lorsque le context que tu utilise pour afficher une dialogue, ou autre n'est pas une Activity... Il faut toujours utiliser le context de l'activity.


    Dans ton code :

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    @Override
    	protected Dialog onCreateDialog(int id) {
    		switch (id) {
    		case DATE_DIALOG_ID:
    			return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,mDay);
    		}
    		return null;
    	}
    le "this" design le Context de TabbeActivity! Est ce que TabbeActivity s'etends d'une Activty ou une sous-classe de Activity?

  5. #5
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    TabbedActivity hérite de Activity

  6. #6
    Expert éminent

    Avatar de Feanorin
    Profil pro
    Inscrit en
    Avril 2004
    Messages
    4 589
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Avril 2004
    Messages : 4 589
    Points : 9 149
    Points
    9 149
    Par défaut
    Bonjour,

    L'erreur comme te l'as indiqué jahbromo provient du fait que tu affiches un composant sur un context non actif.

    Pourrais tu essayer de débogger ton application (breakpoint ou log ) pour savoir quel composant pose problème ainsi à quel endroit ?

    Merci .
    Responsable Android de Developpez.com (Twitter et Facebook)
    Besoin d"un article/tutoriel/cours sur Android, consulter la page cours
    N'hésitez pas à consulter la FAQ Android et à poser vos questions sur les forums d'entraide mobile d'Android.

  7. #7
    Membre régulier
    Profil pro
    Inscrit en
    Mars 2007
    Messages
    541
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Mars 2007
    Messages : 541
    Points : 124
    Points
    124
    Par défaut
    Il fallait que je mette le contexte du parent pour résoudre le problème du DatePicker:

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    @Override
    	protected Dialog onCreateDialog(int id) {
    		switch (id) {
    		case DATE_DIALOG_ID:
    			return new DatePickerDialog(this.getParent(), mDateSetListener, mYear, mMonth,mDay);
    		}
    		return null;
    	}
    et pour le spinner:


    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    View viewToLoad = LayoutInflater.from(this.getParent()).inflate(R.layout.departuretime, null);
    		setContentView(viewToLoad);

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

Discussions similaires

  1. [RTFEditorKit] Problème d'utilisation
    Par jean.lamy dans le forum Entrée/Sortie
    Réponses: 7
    Dernier message: 21/10/2004, 18h30
  2. Problème d'utilisation de Mysql avec dev-c++
    Par Watchi dans le forum Dev-C++
    Réponses: 10
    Dernier message: 06/08/2004, 14h35
  3. [cvs] problèmes d'utilisation
    Par gromite dans le forum Eclipse Java
    Réponses: 3
    Dernier message: 29/06/2004, 17h41
  4. Problème: Requête utilisant NOT IN
    Par fages dans le forum Langage SQL
    Réponses: 4
    Dernier message: 04/05/2004, 10h18
  5. problème d'utilisation avec turbo pascal 7.0
    Par le 27 dans le forum Turbo Pascal
    Réponses: 4
    Dernier message: 03/12/2003, 10h44

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