Novice : Java/Andriod question Overriding

Put your problem here if it does not fit any of the other categories.

Novice : Java/Andriod question Overriding

Postby Mec » Tue Feb 19, 2008 10:35 am

Hi

Its been a few years since I've done any Java Dev and I new to andriod also.
I've been going through some of the examples on this site and I'm having problems getting some of them to compile. This is particularly relating to overriding base functions.

for example (From the Toggle Button example)


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class ToggleButton extends Button {
  3.  
  4.  
  5.  
  6. .............
  7.  
  8. .............
  9.  
  10. .............
  11.  
  12.     /** Return an array of resource IDs of
  13.  
  14.      * the Drawable states representing the
  15.  
  16.      * current state of the view. */
  17.  
  18.     @Override
  19.  
  20.  public int[] OnCreateDrawableState() {
  21.  
  22.          int[] states;
  23.  
  24.          if (this.isChecked()) {
  25.  
  26.               // Checked
  27.  
  28.               states = Button.PRESSED_STATE_SET;
  29.  
  30.          } else {
  31.  
  32.               // Unchecked
  33.  
  34.               if (super.hasFocus()) {
  35.  
  36.                    /* Unchecked && Focus
  37.  
  38.                     * System highlights the Button */
  39.  
  40.                    states = super.OnCreateDrawableState();
  41.  
  42.                  
  43.  
  44.               } else {
  45.  
  46.                    // Unchecked && noFocus
  47.  
  48.                    states = Button.LAST_STATE_SET;
  49.  
  50.               }
  51.  
  52.          }
  53.  
  54.          return states;
  55.  
  56.     }
  57.  
  58. }
  59.  
  60.  
  61.  
  62.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


Both the definition and call to OnCreateDrawableState generate the following errors

The method OnCreateDrawableState() is undefined for the type Button
The method OnCreateDrawableState() of type FocusButton must override a superclass method



I have the same issue when trying to compile the IconTextMenu example by sommeralex

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class IconTextMenuView extends LinearLayout implements
  3.  
  4.                 android.view.View.OnClickListener {
  5.  
  6.  
  7.  
  8. ...........
  9.  
  10. ...........
  11.  
  12. ...........
  13.  
  14.         @Override
  15.  
  16.           boolean onMotionEvent(MotionEvent event) {
  17.  
  18.                 performClick();
  19.  
  20.                 return super.onMotionEvent(event);
  21.  
  22.         }
  23.  
  24. }
  25.  
  26.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Here the OnMotion override fails with
The method onMotionEvent(MotionEvent) is undefined for the type LinearLayout
The method onMotionEvent(MotionEvent) of type IconTextMenuView must override a superclass method


Perhaps I'm missing something obvious or maybe the implementation of the base classes has changed since these tutorials where created Im not sure. I'd be grateful if anyone could point me in the right direction.
Mec
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 19, 2008 10:18 am

Top

Postby Ishtar » Tue Feb 19, 2008 11:11 am

Hi

I think the method is called onCreateDrawableState not OnCreateDrawableState. In the second problem you're not overriding, you are implementing an interface. onMotionEvent is a function of the interface OnClickListener. Just remove the @override tag.

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby Mec » Tue Feb 19, 2008 11:29 am

Thanks for the reply Ishtar

The OnCreateDrawableState rather than onCreateDrawableState error was a mistake on my part I was changing the function names before I posted this and forgot to rectify then.

Even with it set to onCreateDrawableState the same/similar issue remains.


The method onCreateDrawableState() of type ToggleButton must override a superclass method
The method onCreateDrawableState(int) in the type View is not applicable for the arguments ()



Relating to the OnMotion override, you are correct and I removed the @Override

however the line below still generates this error

The method onMotionEvent(MotionEvent) is undefined for the type LinearLayout
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. return super.onMotionEvent(event);
Parsed in 0.034 seconds, using GeSHi 1.0.8.4
Mec
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 19, 2008 10:18 am

Postby Ishtar » Tue Feb 19, 2008 11:40 am

wokidoki
protected int[] onCreateDrawableState(int extraSpace)
Generate the new Drawable state for this view. This is called by the view system when the cached Drawable state is determined to be invalid. To retrieve the current state, you should use getDrawableState().
Parameters
extraSpace if non-zero, this is the number of extra entries you would like in the returned array in which you can place your own states.

Returns
Returns an array holding the current Drawable state of the view.
See Also
mergeDrawableStates(int[], int[])


So, it has to be
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    @Override
  2.  protected int[] onCreateDrawableState(int extraspace) {
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


Mind the protected, and the int argument. Don't ask me what this argument is for... never used this part of the api.

Part2: I don't think you know interfaces, you'd better read a bit about it. There is no super.function(); when implementing an interface, because, you are implementing not overriding.

I have the same issue when trying to compile the IconTextMenu example by sommeralex


Can you post the link?

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Postby Mec » Tue Feb 19, 2008 11:57 am

Thank you again Ishtar for taking the time to look at these issues

your suggestions have been very helpful and I will certainly do a bit more reading/investigation in the future.

FYI the link for IconTextMenu thread is

http://www.anddev.org/how_to_realise_a_grid_of_views-t772.html

and the code is from a few posts down in that thread where "plusminus" submitted a fix for the issues under discussion in the thread


http://www.anddev.org/download.php?id=98
Mec
Freshman
Freshman
 
Posts: 3
Joined: Tue Feb 19, 2008 10:18 am

Postby Ishtar » Tue Feb 19, 2008 2:01 pm

aha! Ishtar has found the problem, in the old SDK, View implemented the onMotionListener, in the new SDK it doesn't. So, the old code doesn't work anymore. Drop the super. and override tag, it's only valid in the old sdk. (I remember a thread where i wondered why it even implemented it, since it did nothing.)

No problem, happy coding!

Ishtar
Ishtar
Experienced Developer
Experienced Developer
 
Posts: 50
Joined: Wed Jan 16, 2008 10:22 pm
Location: Netherlands

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 9 guests