Ok, that mostly makes sense. So my next question is how do I make the button a child of my custom view so that I can use View.findViewById ()?
On Apr 6, 10:14 am, Greg Krimer <[email protected]> wrote: > Activity.findViewById() is different from View.findViewById(). > > The former uses the View set via setContentView() as the root and the > later uses this View as the root. In other words, in order for ... > > findViewById(R.id.checkbox); > > ... to return a non-null View the button must be a child of your view. > > On Apr 5, 4:24 pm, xspotlivin <[email protected]> wrote: > > > I'm trying to create a button in a custom view that I've created. I'm > > looping through an XML document and drawing certain things. I want to > > be able to also create buttons to go in certain locations depending on > > the xml data. I'd like to do it in the view because I want to be able > > to call invalidate() on my view to redraw things and replace the > > buttons when new xml data is available. > > > The problem is that I put my code in the view class, the activity > > crashes because the button is "null". Here is a subset of my code: > > > Java: > > �...@override > > protected void onDraw(Canvas canvas) { > > > // Clock-size rectangle to draw compliance windows > > RectF clockRect = new RectF(86, 6, 394, 314); > > > // Create a node list containing each reminder > > NodeList reminderNodes = this.clockXML.getElementsByTagName > > ("AdherenceInterval"); > > int reminderLength = reminderNodes.getLength(); > > > Button checkbox = (Button) findViewById(R.id.checkbox); > > checkbox.setLayoutParams(new AbsoluteLayout.LayoutParams > > (LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0)); > > checkbox.setOnClickListener(new Button.OnClickListener() { > > public void onClick(View v) { > > Context context = v.getContext(); > > Intent i = new Intent(context, > > ReportMedications.class); > > context.startActivity(i); > > } > > }); > > > I know this code works for creating buttons because I can put it in my > > activity class and it works just fine. Any suggestions? I thought > > about relocating the button in the activity, but I can't figure out > > how to do this when new data is available. > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

