Button in Custom View

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

Button in Custom View

Postby xspotlivin » Mon Apr 06, 2009 12:23 am

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:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         @Override
  2.  
  3.         protected void onDraw(Canvas canvas) {         
  4.  
  5.                
  6.  
  7.                 // Clock-size rectangle to draw compliance windows
  8.  
  9.                 RectF clockRect = new RectF(86, 6, 394, 314);
  10.  
  11.                
  12.  
  13.                 // Create a node list containing each reminder
  14.  
  15.                 NodeList reminderNodes = this.clockXML.getElementsByTagName("AdherenceInterval");
  16.  
  17.                 int reminderLength = reminderNodes.getLength();
  18.  
  19.                
  20.  
  21.                 Button checkbox = (Button) findViewById(R.id.checkbox);
  22.  
  23.         checkbox.setLayoutParams(new AbsoluteLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, 0, 0));
  24.  
  25.         checkbox.setOnClickListener(new Button.OnClickListener() {
  26.  
  27.                 public void onClick(View v) {
  28.  
  29.                         Context context = v.getContext();
  30.  
  31.                         Intent i = new Intent(context, ReportMedications.class);
  32.  
  33.                         context.startActivity(i);
  34.  
  35.                 }
  36.  
  37.         });
  38.  
  39.                
  40.  
  41.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


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.
xspotlivin
Junior Developer
Junior Developer
 
Posts: 23
Joined: Thu Feb 26, 2009 8:15 am
Location: Cambridge, MA

Top

Postby WarrenFaith » Tue Apr 07, 2009 8:03 am

findViewById(R.id.mything) returns null in two cases:
1. there is no content view (method setContentView(R.layout.whatever) was never called)
or 2. there is simply no R.id.mything
WarrenFaith
Moderator
Moderator
 
Posts: 227
Joined: Fri Mar 13, 2009 10:59 am
Location: Berlin, Germany

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 5 guests