tomy29 Posted May 26, 2010 Share Posted May 26, 2010 Bonjour, J'ai le code suivant (simple bouton qui affiche un Dialog qui contient un editText et un Button. Mon soucis c'est que (Button) findViewById(R.id.Button01) est Null alors que R.id.Button01 a bien une valeur public class Main extends Activity implements OnClickListener { Button btnCreate; String returnedValue=""; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); btnCreate = (Button) findViewById(R.id.btnCreate); btnCreate.setOnClickListener(this); } @Override public void onClick(View component) { switch (component.getId()){ case R.id.btnCreate : showCreateDialog(); EditText edReturnedValue = (EditText)findViewById(R.id.editReturnedValue); edReturnedValue.setText(returnedValue); } } private void showCreateDialog(){ final Dialog alert = new Dialog(this); alert.setContentView(R.layout.mainpopup); Button buttonValid = (Button) findViewById(R.id.Button01); buttonValid.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { EditText inputBox1 =(EditText)alert.findViewById(R.id.EditText01); alert.show(); returnedValue = inputBox1.getText().toString(); alert.dismiss(); } }); } } Je ne comprends pas pourquoi, si vous pouvez m'aider.... Merci! Quote Link to comment Share on other sites More sharing options...
naholyr Posted May 26, 2010 Share Posted May 26, 2010 (edited) Dans "res/layout/mainpopup.xml", je parie qu'il n'y a pas de tag "" :) Edit : ah si ok j'ai compris ! Tu fais un simple "findViewById" qui correspond à "this.findViewById", c'est à dire que tu cherches ta vue dans le layout que tu as appliqué dans ton activité principale, qui ne contient pas de Button01. Si tu as fait le setContentView sur l'objet X c'est X.findViewById que tu dois appeler (ici alert.findViewById(...)). Edited May 26, 2010 by naholyr Quote Link to comment Share on other sites More sharing options...
tomy29 Posted May 26, 2010 Author Share Posted May 26, 2010 voila, merci ;) par contre quand je debug au pas a pas, quand je suis rendu au buttonValid.setOnClickListener(new OnClickListener(), il ne se passe plus rien ensuite, je ne rentre pas dans le onClick(). Une idée? Quote Link to comment Share on other sites More sharing options...
naholyr Posted May 26, 2010 Share Posted May 26, 2010 Tel que je comprends ton code, tu crées un dialogue, tu récupères un bouton dans ce dialogue, tu dis qu'au clic sur ce bouton on affiche ledit dialogue, et tu n'affiches pas le dialogue, donc le bouton n'apparait pas, donc personne ne peut cliquer dessus, donc le dialogue n'est pas prêt d'apparaître... Y a pas comme un souci dans ta conception ? :P Quote Link to comment Share on other sites More sharing options...
tomy29 Posted May 27, 2010 Author Share Posted May 27, 2010 (edited) Effectivement, avec un alert.show() au bon endroit, c'est mieux :) Edited May 27, 2010 by tomy29 Quote Link to comment Share on other sites More sharing options...
nini29 Posted December 7, 2010 Share Posted December 7, 2010 (edited) bonjour j'ai le même problème. - Mon fichier java est : public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); button1 = (ImageButton) findViewById(R.id.button1); - Mon fichier xml est la suivante : <?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout android:orientation="vertical" android:id="@+id/widget0" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/fond" android:layout_marginTop="0px" android:layout_marginBottom="0px" xmlns:android="http://schemas.android.com/apk/res/android" > <ImageButton android:id="@+id/button1" android:layout_width="70px" android:layout_height="70px" android:scaleType="fitXY" android:background="@android:color/transparent" android:src="@drawable/logo1" android:layout_x="70px" android:layout_y="70px" > </AbsoluteLayout> - Enfin mon fichier r.java contient : public static final class id { public static final int button1=0x7f050001; public static final int button2=0x7f050002; } Je ne voit pas ou est l'erreur pourtant quand je passe sur la ligne button1 = (Button) findViewById(R.id.button1); l'emulateur me force a fermer merci d'avance de vos réponces Edited December 7, 2010 by nini29 Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.