What I have here is a custom view and then I want some widgets to go with it.
<com.zone.manager.Tab3
android:id="@+id/tab3_display"
android:layout_width="fill_parent"
android:layout_height="620dp" >
<Button
android:id="@+id/addZone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Zone" />
</com.zone.manager.Tab3>
Now I want to have that button set to onclicklistener so I can do stuff with it in the View class so I did this…
addZone = (Button) findViewById(R.id.addZone);
addZone.setOnClickListener(this);
I set that in the
public class Tab3 extends ViewGroup implements OnTouchListener, OnClickListener
Public Tab3(Context context, AttributeSet attrs)
{
super (context, attrs);
// here
}
When I extended ViewGroup it made me implement this
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
// TODO Auto-generated method stub
}
Is there something that I am suppsot to put in here in order to make this work?
But when I try to run the app it crashes, however if I // out the addZone.setOnClickListener(this); the app runs fine, any help for me?
tabs
th.setup();
TabSpec specs = th.newTabSpec("tag0");
specs.setContent(R.id.connecttionTab);
specs.setIndicator("Connection Tab");
th.addTab(specs);
specs = th.newTabSpec("tag1");
specs.setContent(R.id.tab1);
specs.setIndicator("Zone Manager");
th.addTab(specs);
specs = th.newTabSpec("tag2");
specs.setContent(R.id.tab2);
specs.setIndicator("",res.getDrawable(R.drawable.ic_tab_vaccontrol));
th.addTab(specs);
//this is the tab that has all this vvv
specs = th.newTabSpec("tag3");
specs.setContent(R.id.tab3);
specs.setIndicator("Graphical Layout");
th.addTab(specs);
In your xml, your custom view does not contain the button; it is a sibling view. The reason your app crashes is that
findViewById(R.id.addZone)is returningnull, so you are getting aNullPointerExceptionwhen you calladdZone.setOnClickListener(this). If you want your custom view to contain the button, the xml would have to look something like this:Also, your
Tab3class will have to extendViewGroup, notView. This can get a bit complicated because you also need to write code to do the layout. The button will also be displayed inside theTab3view.EDIT
Based on your comments of what you are trying to do, I do not recommend the above approach. Instead, you should just wrap your custom view and the Button in a
LinearLayout. For example, the following layout will put the button at the bottom left of the screen and will have a Tab3 view fill the area above it:res/layout.tab3.xml
Then, move the click processing logic to a separate method in
Tab3that is visible to yourActivityclass. (Let’s assume that it is calledaddZone().) TheTab3class should not implementOnClickListenerand should extendView(notViewGroupas above). By adding theandroid:onClickattribute to the button, you don’t need to add anOnClickListenerto the button. Instead, you need to implement a click method of that name in the activity:Although there is nothing in your code about the button, the framework will automatically wire everything up using reflection so that the
onAddZonemethod of the activity will be called when the button is clicked.