Jump to content

findViewById retourne null


tomy29

Recommended Posts

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!

Link to comment
Share on other sites

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 by naholyr
Link to comment
Share on other sites

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

Link to comment
Share on other sites

  • 6 months later...

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 by nini29
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...