Android-User Posted February 27, 2011 Share Posted February 27, 2011 Bonsoir, J'ai fait une application ayant un menu et un bouton permettant d'intégrer le contenu d'une activité. Cependant, je ne sais pas comment faire fonctionner ce bout de code ci-dessous dans le FirstActivity parce qu'il est impossible d'utiliser 2 fois onCreate etc... Peut-on utiliser d'autres alternatives à OnCreate ? Par exemple, OnCreate2 ? Bout de code à intégrer dans FirstActivity, permettant de charger le contenu de mon SecondActivity @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button next = (Button) findViewById(R.id.Button01); next.setOnClickListener(new View.OnClickListener() { public void onClick(View view) { //Intent myIntent = new Intent(view.getContext(), SecondActivity.class); //startActivityForResult(myIntent, 0); Intent intent = new Intent(FirstActivity.this, SecondActivity.class); startActivity(intent); } }); } FirstActivity.java package com.test; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.widget.Button; import android.widget.Toast; public class FirstActivity extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.menu, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show(); break; case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show(); break; case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show(); FirstActivity.this.finish(); break; } return true; } } Merci d'avance !! Link to comment Share on other sites More sharing options...
chpil Posted February 28, 2011 Share Posted February 28, 2011 Pourquoi veux-tu ajouter un deuxième onCreate dans FirstActivity ? Je ne comprends pas ce que tu veux faire... Link to comment Share on other sites More sharing options...
Sakaroz Posted February 28, 2011 Share Posted February 28, 2011 Ya la méthode onResume() si tu veux.... mais je comprends pas non plus le fond de ton problème Link to comment Share on other sites More sharing options...
Android-User Posted February 28, 2011 Author Share Posted February 28, 2011 Merci d'avoir répondu ! Mon problème étant de vouloir faire fonctionner 2 activités, mais je ne sais pas comment m'y prendre... Dans mon FirstActivity, c'est simplement un menu, mais j'aimerai "appeler" mon autre activité qui est un bouton... Faut-il l'insérer dans mon FirstActivity ou utiliser un intent pour l'intégrer ? Merci d'avance ! Link to comment Share on other sites More sharing options...
chpil Posted February 28, 2011 Share Posted February 28, 2011 Si dans ta FirstActivity, tu as un menu pour appeler ta SecondActivity, il te suffit d'implémenter le code nécessaire pour le faire (new Intent()/startActivity), dans la méthode onOptionsItemSelected de ta FirstActivity Link to comment Share on other sites More sharing options...
Android-User Posted February 28, 2011 Author Share Posted February 28, 2011 Merci d'avoir répondu, mais cela me donne une erreur lorsque je lance mon application sur l'émulateur... public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.icon: Toast.makeText(this, "You pressed the icon!", Toast.LENGTH_LONG).show(); Intent intent = new Intent(FirstActivity.this, ThirdActivity.class); startActivity(intent); break; case R.id.text: Toast.makeText(this, "You pressed the text!", Toast.LENGTH_LONG).show(); break; case R.id.icontext: Toast.makeText(this, "You pressed the icon and text!", Toast.LENGTH_LONG).show(); FirstActivity.this.finish(); break; } return true; } J'ai ajouté un startactivity dans case R.id.icon Est-ce correct ? Merci d'avance ! :) Link to comment Share on other sites More sharing options...
chpil Posted February 28, 2011 Share Posted February 28, 2011 Et quelle est l'erreur qui se produit ? (parce que si tu ne nous l'indiques pas, on va pas deviner tout seul ;) ) A priori, le code est correct, ce qui ne veut pas dire qu'il va fonctionner. Par exemple, as-tu pensé à déclarer ton Activity (ThirdActivity) dans ton androidManifest.xml (toute Activity doit y être déclarée) ? Link to comment Share on other sites More sharing options...
Android-User Posted February 28, 2011 Author Share Posted February 28, 2011 Ah cela fonctionne ! :) En effet, j'ai oublié d'ajouter ceci : <activity android:name=".ThirdActivity" /> Merci de votre aide ! Link to comment Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.