Simply trying to extend View and do some custom work, but Eclipse will complain when I attempt to override the setFrame method. Claiming there isn't a method in the parent class to override:
The method setFrame(int, int, int, int) of type Test must override or implement a supertype method
Here is the signature of the method from android SDK source.
protected boolean setFrame(int left, int top, int right, int bottom)
As you can see it's not private or package level, or even specified as final... just protected. Which should开发者_如何学C mean I am totally able to override it in a subclass. Right? Below is the bare minimum of what I'm trying to do in Eclipse. Perhaps it is just an Eclipse error, but I'm not too familiar with using Ant to check against that.
Edit: To those answering that setFrame is not defined in the View class, I can assure you it is. How else do you think I got the method signature? It is even called during layout(). Or am I seriously just crazy?
git HEAD: View.java
Cupcake (1.5r4): View.javaYou can even see the method being overriden in the ImageView and TextView classes...this is why I am seriously confused as to why I cannot override it myself from View directly...
public class Test extends View {
public Test(Context context) {
super(context);
}
public Test(Context context, AttributeSet attrs) {
super(context, attrs);
}
public Test(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
protected boolean setFrame(int left, int top, int right, int bottom) {
return super.setFrame(left, top, right, bottom);
}
}
According to the documentation, setFrame
is not defined in the View
class (not strictly true - see edit). Surprising, each subclass, TextView
and ImageView
, define it themselves. You'll have to have to extend the specific subclass for each widget you want to override this behavior. This is based on the docs for Android 2.3 r1 - 05 Jan 2011 12:43
.
See the documentation:
Classes that define setFrame
http://www.google.com/search?q=site:developer.android.com+%22boolean+setFrame%22
TextView and ImageView.
Edit:
As the OP points out in the comments, the method is clearly defined in the View.java
source code. However, the documentation acts as if the method isn't defined there.
The reason for this is that the setFrame() method in View
has the @hide Javadoc tag:
/**
* Assign a size and position to this view.
*
* This is called from layout.
*
* @param left Left position, relative to parent
* @param top Top position, relative to parent
* @param right Right position, relative to parent
* @param bottom Bottom position, relative to parent
* @return true if the new size and position are different than the
* previous ones
* {@hide}
*/
protected boolean setFrame(int left, int top, int right, int bottom) {
Apparently, this hides the method from the Javadoc:
http://www.androidjavadoc.com/?p=63
The especial [sic] attention is need to turn to the @hide tag which standard doclet can’t interpret and which hides non-SDK source and thus this code shouldn’t be used in applications.
Is it possible that the reason it can't be overridden is that the Eclipse plug-in for Android or the Android compiler is somehow enforcing the @hide
tag? I don't know.
Because setFrame is not defined in the View class
精彩评论