I'm very new to Android programming and I am using Android Studio to target a Nexus 9. So far it's been a good/great experience.
I'm finally encountering some odd behavior though, when I tried something a little advanced -- sorry for the oddball question, I'd be happy with just some troubleshooting tips, since the observations don't give me much to go on. Here goes...
I have one class derived from another, as follows:
cWidget
, which has methodOnMyEvent()
cFrame
extendscWidget
, overridesOnMyEvent()
Each of my cWidgets
(and hence my cFrames
) has a linked list of "child" cWidgets
(and/or cFrames
), to form a tree structure. In both my cWidget.OnMyEvent()
method and the cFrame.OnMyEvent()
override I loop through the child cWidgets
and call the OnMyEvent()
on each -- so that my event is kind of "passed down" through the hierarchy by traversing the tree. My hope is that if the child is a cWidget
it calls cWidget.OnMyEvent()
and if it's actually a cFrame
it calls the cFrame.OnMyEvent()
override (this is how it would work in .NET, I should write some code to verify this is how it works here in Java but I realize now this is currently an assumption).
The problem: when I debug and set a breakpoint in cWidget.OnMyEvent()
it never fires, even though there are definitely cWidgets
in the tree. When I breakpoint on the call from cFrame.OnMyEvent()
to a child cWidget.OnMyEvent()
, and I inspect all the local variables, everything looks right; ie the child is a cWidget
as expected, and nothing is null... but if I resume execution it still does not trip the breakpoint in the cWidget.OnMyEvent()
as expected, it just passes over it. Even weirder, if I "Step Into" the call to cWidget.OnMyEvent()
, my application halts with an "Unfortunately, MyFirstApp has stopped
", and no exception report in my logcat.
So... very sorry for the long description but I'm not sure what's important and what's not. Without an exception report I'm not sure how to treat this problem, and there is some chance I am breaking some rules by linking together parents and children in the same tree and hoping Java knows whether to call the base method or the override. (This worked in .NET, and so far everything there has worked here but maybe not in this case.)
Thanks a lot for any thoughts or troubleshooting tips.
EDIT: I boiled it down and tested it, get similar results but still don't know why. I defined cA with an AddChild and Handle method, then derived cB and overrode Handle. I created a tree (two cAs as children of a cB) and then called cB handle. When I try to build a tree and call Handle it crashes. I'm guessing I'm trying a .NET trick that is disallowed here.
// *************
// BASE CLASS WITH AddChild, Handle
// *************
public class cA
{
public int m_Tag = 0;
protected cA ptr_FirstChild = null;
protected cA ptr_LastChild = null;
protected cA ptr_Parent = null;
protected cA ptr_NextSibling = null;
public cA(int tag)
{
m_Tag = tag;
}
public void AddChild(cA a)
{
a.ptr_Parent = this;
if (ptr_FirstChild == null)
{
ptr_FirstChild = a;
ptr_LastChild = a;
}
else
{
ptr_LastChild.ptr_NextSibling = a;
ptr_LastChild = a;
}
}
public void Handle()
{
int a;
a=3;
cA tmp = ptr_FirstChild;
while (tmp!= null)
{
tmp.Handle();
tmp = tmp.ptr_NextSibling;
}
}
}
// *************
// DERIVED CLASS, overrides Handle
// *************
public class cB extends cA
{
public cB(int tag)
{
super(tag);
}
@Override
public void Handle()
{
int a;
a=4;
cA tmp = ptr_FirstChild;
while (tmp!= null)
{
tmp.Handle();
tmp = tmp.ptr_NextSibling;
}
}
}
// *************
// Usage of classes
// build a tree with both cAs and cBs, then call
// root.Handle, hoping to traverse the tree.
// *************
public cA ptr_Root; // define the 'root' of the tree
cA a1 = new cA(1); // instantiate all leaves
cA a2 = new cA(2);
cB b1 = new cB(1);
ptr_Root = b1; // build the tree
b1.AddChild(a1);
b1.AddChild(a2);
b1.Handle(); // call Handle on the root, intending to traverse the tree, but this halts the program