You're inflating a whole layout, then attaching a listener to it, then throwing the whole layout away. There's nothing here showing that the layout you've inflated is actually shown or attached to the current activity. If it's not on the screen, it's not going to react to clicks.
Normally, you call your activity's .setContentView() with an ID of a layout (R.layout.level), and that does the inflating and more importantly, the continual display of said layout. You can then use .findViewById() to grab various buttons or other view types in that layout. setContentView(R.layout.level); Button updateLevel = findViewById(R.id.updateLevel); If you are inflating portions of a whole activity's layouts, then be sure to add those inflated views to the activity's existing containers (layouts). For example, setContentView(R.layout.whatever); ViewGroup group = (ViewGroup)findViewById(R.id.levelgroup); View level = getLayoutInflater() .inflate(R.layout.level, levelgroup, false); levelgroup.addView(level); Button updateLevel = levelgroup.findViewById(R.id.updateLevel); On Mar 24, 7:58 pm, The young programmer <ayoungprogram...@gmail.com> wrote: > All my other On Click methods work except the ones the I have to > inflate the layout "to get the button". What should I do to make this > work? Or is it just my code that is wrong? > > Here are how I define my On Click listeners for the problem buttons: > > Button updateLevel = > (Button)getLayoutInflater().inflate(R.layout.level, > null).findViewById(R.id.updateLevel); > > updateLevel.setOnClickListener(new View.OnClickListener() { > > @Override > public void onClick(View v) { > setLevelOnClick(v); > > } > }); > > Button goBackMainMenu = (Button) > getLayoutInflater().inflate(R.layout.play, > null).findViewById(R.id.tomenu); > goBackMainMenu.setOnClickListener(new View.OnClickListener() { > > @Override > public void onClick(View v) { > toMenuOnClick(v); > > } > }); > > Here are my onClick actions: > > protected void toMenuOnClick(View v) { > setContentView(R.layout.main); > > } > > protected void setLevelOnClick(View v) { > > setContentView(R.layout.main); > > } > > What is wrong? -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en