You Are Here:

Community: Developer Discussion Boards

#1 Old "public class" is not abstract and does not override abstract method commandAction - 2008-12-09, 11:30

Join Date: Dec 2008
Posts: 45
werter10
Offline
Registered User
Hi,

My project consist in a client MIDlet that connects with a server, and a public class written in a separate file that implements a voice recorder.
There is no problem with the MIDlet, but the recorder cannot be compiled, because it has some errors.

First error appears when I declare the public class "grabadora"(means recorder):
"public class" is not abstract and does not override abstract method commandAction(javax.microedition.lcdui.Command,javax.microedition.lcdui.Displayable)in javax.microedition.lcdui.CommandListener


One of my problems is I don't know how to build a project in different files correctly. Where can I find some info about that?

My code is:




import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.Display.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;
import java.io.*;

public class grabadora implements javax.microedition.lcdui.CommandListener, PlayerListener {
ByteArrayOutputStream outputStream =null;
byte[] byteArray = null;
String locator= "capture://audio";
Player player =null;
Form form;
TextField tb = null;
Command recCommand, stopRecCommand, playCommand,exitCommand;
Display display=null;

RecordControl rc=null;
ChoiceGroup cg;
/** Creates a new instance of HelloMidlet */
public grabadora() { //constructor

cg= new ChoiceGroup ("Record to", ChoiceGroup.EXCLUSIVE);
cg.append("file",null);
cg.append("byteArray", null);
cg.setSelectedIndex(1, true);
form = new Form("Audio Capture");

recCommand = new Command("record", Command.OK, 1);
stopRecCommand = new Command("stop record", Command.OK, 1);
playCommand = new Command("Play", Command.SCREEN, 2);
exitCommand = new Command("Exit", Command.SCREEN, 2);

tb = new TextField("SoundCapture", "info", 256, TextField.UNEDITABLE) ;
form.addCommand(recCommand);
form.addCommand(playCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);

form.append(tb);
form.append(cg);
display.setCurrent(form); //Lo he puesto yo

}
public void commandAction(Command c, Displayable s) {

// Making a boolean vector to get the options from the choicegroup
boolean flags[] = new boolean[2];
flags[0]=false;
flags[1]=false;

cg.getSelectedFlags(flags);
if (c == recCommand) // start the recording
{
System.out.println("recCommand");
tb.setString("Recorder started:");
// Changing the recCommand with the stopCommand
form.removeCommand(recCommand);
form.addCommand(stopRecCommand);

try{
// creating the player from the file url enabling progressive download
player = Manager.createPlayer(locator);
// adding playerListenter
player.addPlayerListener(this);
// allocating the resources from the microphone
player.realize();
// Getting the ReocrdControll so we can record the output from the player
rc = (RecordControl)player.getControl("RecordControl");
}
catch(Exception er){tb.setString(er.toString());}

if(flags[0]) // If we are saving the recorde audio to file
{
try{
// setting the record location to the file
rc.setRecordLocation("file:///c:/other/audio.amr");
// start recording
rc.startRecord();
// start to play the sound from the microphone
player.start();
}catch(Exception t){tb.setString(t.toString());}
}
else // If we are saving to an OutputStream
{
outputStream = null;
outputStream = new ByteArrayOutputStream();

if(rc==null)
System.out.println("rc==null");
// setting the record location to the outputstream
rc.setRecordStream(outputStream);

try{
// start recording
rc.startRecord();
// start to play the sound from the microphone
player.start();

}
catch(Exception e){tb.setString(e.toString());}

}


}
else if (c == stopRecCommand) // Stop the recording
{
System.out.println("StoprecCommand");
// Changing the stopCommand with recCommand
form.removeCommand(stopRecCommand);
form.addCommand(recCommand);
try{

// stop recording
rc.commit();
// and stop the playback from the microphone
player.close();

tb.setString("Recorder to byteArray stopped:");
}
catch(Exception e){tb.setString(e.toString());}
}
else if (c == playCommand) // start the playback of the recorded audio
{

if(flags[0]) // to File
{
System.out.println("play from File");
// creating and starting the playback thread
new PlayerThread("file:///c:/other/audio.amr").start();

}
else // to outputStream
{
System.out.println("play from ByteArrayInputStream");
if(outputStream==null)
return;
// getting the ByteArray from the ByteArrayOutputStream
byteArray = outputStream.toByteArray();
// Creating and starting the playback thread
new PlayerThread(new ByteArrayInputStream(byteArray), "audio/amr" ).start();

}
tb.setString("Playing ");
}
else if (c == exitCommand)
{

}

}

public void playerUpdate(Player player,java.lang.String event,java.lang.Object eventData)
{
System.out.println("playerUpdate event="+event);

}

}






Any suggestions?, thanks a million
Last edited by werter10 : 2008-12-09 at 12:32.
Reply With Quote

#2 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:12

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
Hmmmm... looks OK to me... my first guess is that you have something unusual in your class path.

What is your classpath when compiling?

What are the names of the other classes in your application?
Reply With Quote

#3 Old Thumbs up Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:21

Join Date: Mar 2008
Posts: 2,161
Location: The Capital of I N D I A
Send a message via Skype™ to raj_J2ME
raj_J2ME's Avatar
raj_J2ME
Offline
Forum Nokia Champion
Hi,
Just read this paragraph,

An interface in the Java programming language is an abstract type that is used to specify an interface (in the generic sense of the term) that classes must implement. Interfaces are declared using the interface keyword, and may only contain method signatures and constant declarations (variable declarations which are declared to be both static and final). An interface may never contain method definitions.

As interfaces are implicitly abstract, they cannot be directly instantiated. Object references in Java may be specified to be of an interface type; in which case they must either be null, or be bound to an object which implements the interface.

Quote:
The keyword implements is used to declare that a given class implements an interface. A class which implements an interface must either implement all methods in the interface, or be an abstract class.
One benefit of using interfaces is that they simulate multiple inheritance. All classes in Java (other than java.lang.Object, the root class of the Java type system) must have exactly one base class; multiple inheritance of classes is not allowed. However, a Java class/interface may implement/extend any number of interfaces.

Therefore you have to implement the CommandAction() method of the interface CommandListener

Hope these lines can help you,


Thanks

R a j - The K e r n e l
Reply With Quote

#4 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:26

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
raj... he knows that... see the line:

Code:
public void commandAction(Command c, Displayable s) {
in the code he posted.
Reply With Quote

#5 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:28

Join Date: Dec 2008
Posts: 45
werter10
Offline
Registered User
Quote:
Originally Posted by grahamhughes View Post
Hmmmm... looks OK to me... my first guess is that you have something unusual in your class path.

What is your classpath when compiling?

What are the names of the other classes in your application?

Hi, thanks for your fast reply.

I have three different files in the source packages: Echoclient.java (MIDlet), grabadora.java (voice recorder), PlayerThread.java.
Grabadora works fine if I compile as a MIDlet, but I think I made a mistake when I modified to convert it to a public class for my application.

thanks
Reply With Quote

#6 Old Thumbs up Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:48

Join Date: Mar 2008
Posts: 2,161
Location: The Capital of I N D I A
Send a message via Skype™ to raj_J2ME
raj_J2ME's Avatar
raj_J2ME
Offline
Forum Nokia Champion
Hi,
Well, the same theory applicable for the abstract classes and other interfaces..I guess he has miss some implementation..against either the abstract class or interface,

Thanks,


Thanks

R a j - The K e r n e l
Reply With Quote

#7 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 12:53

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
My first thought is that there is another class called "Command" in your classpath, and that the parameter "c" in commandAction() is being seen as the wrong class (and so does not implement the interface correctly).

Did you need to fully qualify the name of CommandListener?

Check through the imports (get rid of import javax.microedition.lcdui.Display.* for a start).

Check the class path... Are you compiling from an IDE, or the command line?
Reply With Quote

#8 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 13:15

Join Date: Dec 2008
Posts: 45
werter10
Offline
Registered User
Quote:
Originally Posted by grahamhughes View Post
My first thought is that there is another class called "Command" in your classpath, and that the parameter "c" in commandAction() is being seen as the wrong class (and so does not implement the interface correctly).

Did you need to fully qualify the name of CommandListener?

Check through the imports (get rid of import javax.microedition.lcdui.Display.* for a start).

Check the class path... Are you compiling from an IDE, or the command line?
hi,
well there is another commandAction implemented in the MIDlet,(not in grabadora class) but I thought there wasn't any problem.
I'm compiling from NeatBeans IDE. When you say the classpath, do you mean the system classpath or the path of grabadora public class? How should I check the classpath?. Fully qualify?
I'm new in this stuff, I'm sorry.

I'm explaining the whole project better now. I have a MIDlet wich connects via Bluetooth to a computer, when I accomplished that, I wrote a public class in another file to record some voice and trying to figure out how to send it to the computer. I though about using another file for the recorder, with it own commands for recording and stopping.

Any other idea to structure the project?

Thank you very much
Reply With Quote

#9 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 13:47

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
No problem... quick Java lesson...

A fully qualified class name ("FQN") is like:

Code:
javax.microedition.lcdui.Command
So, the complete name of the class, including the package name. When you specify a simple class name (like Command), the compiler uses the import directives at the head of the source code to find the correct package. "java.lang.*" is imported automatically. The compiler must find the class in exactly one package. The order of the imports makes no difference.

The classpath is different. This is basically a list of directories, JAR or ZIP files, which the compiler uses to find the actual .class files. For example, your code refers to:

Code:
javax.microedition.lcdui.Command
so the compiler must find the file:

Code:
javax\microedition\lcdui\Command.class
somewhere. It will search the directories and JAR/ZIP files in the classpath (in order) until it finds this file. If there are two such files in the path, the compiler finds the first, so the order here is important.

Back to your code...

I tried to compile it, and it's fine. Here's what I did.

There is a reference in the code to PlayerThread. I don't have this, so I created a dummy class, like this:

Code:
import java.io.*;

public class PlayerThread extends Thread {
    public PlayerThread(String s) {
    }
    public PlayerThread(InputStream in, String s) {
    }
}
This is just enough to make the grabadora class compile.

Then I run the compiler, from the command line.

Code:
javac -classpath .;c:\WTK2.5.2\lib\cldcapi11.jar;c:\WTK2.5.2\lib\midpapi20.jar;c:\WTK2.5.2\lib\mmapi.jar;c:\WTK2.5.2\lib\jsr75.jar grabadora.java
In the classpath, I've listed the JAR files that contain the API classes needed for this application. The ".;" at the start of the path makes sure the compiler looks in the current directory (where grabadora.java is) first.

It compiles fine. So, there is not a fault in grabadora.java.

Possibly, this is not the class it is complaining about. I don't use NetBeans so I'm not sure what evil it might be doing. Try compiling from the command line, so you can control exactly what is going on.

Any help?
Reply With Quote

#10 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 18:27

Join Date: Dec 2008
Posts: 45
werter10
Offline
Registered User
Quote:
Originally Posted by grahamhughes View Post
No problem... quick Java lesson...

A fully qualified class name ("FQN") is like:

Code:
javax.microedition.lcdui.Command
So, the complete name of the class, including the package name. When you specify a simple class name (like Command), the compiler uses the import directives at the head of the source code to find the correct package. "java.lang.*" is imported automatically. The compiler must find the class in exactly one package. The order of the imports makes no difference.

The classpath is different. This is basically a list of directories, JAR or ZIP files, which the compiler uses to find the actual .class files. For example, your code refers to:

Code:
javax.microedition.lcdui.Command
so the compiler must find the file:

Code:
javax\microedition\lcdui\Command.class
somewhere. It will search the directories and JAR/ZIP files in the classpath (in order) until it finds this file. If there are two such files in the path, the compiler finds the first, so the order here is important.

Back to your code...

I tried to compile it, and it's fine. Here's what I did.

There is a reference in the code to PlayerThread. I don't have this, so I created a dummy class, like this:

Code:
import java.io.*;

public class PlayerThread extends Thread {
    public PlayerThread(String s) {
    }
    public PlayerThread(InputStream in, String s) {
    }
}
This is just enough to make the grabadora class compile.

Then I run the compiler, from the command line.

Code:
javac -classpath .;c:\WTK2.5.2\lib\cldcapi11.jar;c:\WTK2.5.2\lib\midpapi20.jar;c:\WTK2.5.2\lib\mmapi.jar;c:\WTK2.5.2\lib\jsr75.jar grabadora.java
In the classpath, I've listed the JAR files that contain the API classes needed for this application. The ".;" at the start of the path makes sure the compiler looks in the current directory (where grabadora.java is) first.

It compiles fine. So, there is not a fault in grabadora.java.

Possibly, this is not the class it is complaining about. I don't use NetBeans so I'm not sure what evil it might be doing. Try compiling from the command line, so you can control exactly what is going on.

Any help?

Hi, thanks.
Well, I tried to compile setting in the path one of my JAR files, just to try but it doesn't work. I did:
javac -classpath .;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar grabadora.java

You can read afterwards javac :invalid flag: Files\NetBeans
usage: javac<options> <source files>

The code of grabadora comes from a MIDlet which I adapted to become a class, it wrks as a MIDlet but I did something wrong...

Thanks anyway, I write the original code, just in case...

package hello;

import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.*;
import javax.microedition.media.control.*;
import javax.microedition.io.file.*;
import javax.microedition.io.*;

import java.io.*;
/**
*
* @author 23049734
*/
public class SoundCaptureMIdlet extends MIDlet implements javax.microedition.lcdui.CommandListener, PlayerListener {
ByteArrayOutputStream outputStream =null;
byte[] byteArray = null;
String locator= "capture://audio";
Player player =null;
Form form;
TextField tb = null;
Command recCommand, stopRecCommand, playCommand,exitCommand;
Display display=null;

RecordControl rc=null;
ChoiceGroup cg;
/** Creates a new instance of HelloMidlet */
public SoundCaptureMIdlet () {

cg= new ChoiceGroup ("Record to", ChoiceGroup.EXCLUSIVE);
cg.append("file",null);
cg.append("byteArray", null);
cg.setSelectedIndex(1, true);
form = new Form("Audio Capture");

recCommand = new Command("record", Command.OK, 1);
stopRecCommand = new Command("stop record", Command.OK, 1);
playCommand = new Command("Play", Command.SCREEN, 2);
exitCommand = new Command("Exit", Command.SCREEN, 2);

tb = new TextField("SoundCapture", "info", 256, TextField.UNEDITABLE) ;
form.addCommand(recCommand);
form.addCommand(playCommand);
form.addCommand(exitCommand);
form.setCommandListener(this);

form.append(tb);
form.append(cg);


}

// --- This section is auto-generated by NetBeans IDE. Do not modify or you may lose your changes.//<editor-fold id="MVDMethods" defaultstate="collapsed" desc="This section is auto-generated by NetBeans IDE.">//GEN-BEGIN:MVDMethods
/**
* This method initializes UI of the application.
*/
private void initialize() {
}

// --- This is the end of auto-generated section.//</editor-fold>//GEN-END:MVDMethods

public void startApp() {
if(display==null)
display=Display.getDisplay(this);
display.setCurrent(form);
initialize();
}

public void pauseApp() {
}

public void destroyApp(boolean unconditional) {
}



public void commandAction(Command c, Displayable s) {

// Making a boolean vector to get the options from the choicegroup
boolean flags[] = new boolean[2];
flags[0]=false;
flags[1]=false;

cg.getSelectedFlags(flags);
if (c == recCommand) // start the recording
{
System.out.println("recCommand");
tb.setString("Recorder started:");
// Changing the recCommand with the stopCommand
form.removeCommand(recCommand);
form.addCommand(stopRecCommand);

try{
// creating the player from the file url enabling progressive download
player = Manager.createPlayer(locator);
// adding playerListenter
player.addPlayerListener(this);
// allocating the resources from the microphone
player.realize();
// Getting the ReocrdControll so we can record the output from the player
rc = (RecordControl)player.getControl("RecordControl");
}
catch(Exception er){tb.setString(er.toString());}

if(flags[0]) // If we are saving the recorde audio to file
{
try{
// setting the record location to the file
rc.setRecordLocation("file:///c:/other/audio.amr");
// start recording
rc.startRecord();
// start to play the sound from the microphone
player.start();
}catch(Exception t){tb.setString(t.toString());}
}
else // If we are saving to an OutputStream
{
outputStream = null;
outputStream = new ByteArrayOutputStream();

if(rc==null)
System.out.println("rc==null");
// setting the record location to the outputstream
rc.setRecordStream(outputStream);

try{
// start recording
rc.startRecord();
// start to play the sound from the microphone
// player.start();

}
catch(Exception e){tb.setString(e.toString());}

}


}
else if (c == stopRecCommand) // Stop the recording
{
System.out.println("StoprecCommand");
// Changing the stopCommand with recCommand
form.removeCommand(stopRecCommand);
form.addCommand(recCommand);
try{

// stop recording
rc.commit();
// and stop the playback from the microphone
player.close();

tb.setString("Recorder to byteArray stopped:");
}
catch(Exception e){tb.setString(e.toString());}
}
else if (c == playCommand) // start the playback of the recorded audio
{

if(flags[0]) // to File
{
System.out.println("play from File");
// creating and starting the playback thread
new PlayerThread("file:///c:/other/audio.amr").start();

}
else // to outputStream
{
System.out.println("play from ByteArrayInputStream");
if(outputStream==null)
return;
// getting the ByteArray from the ByteArrayOutputStream
byteArray = outputStream.toByteArray();
// Creating and starting the playback thread
new PlayerThread(new ByteArrayInputStream(byteArray), "audio/amr" ).start();

}
tb.setString("Playing ");
}
else if (c == exitCommand)
{
notifyDestroyed();
}

}

public void playerUpdate(Player player,java.lang.String event,java.lang.Object eventData) {
System.out.println("playerUpdate event="+event);

}

}
Reply With Quote

#11 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2008-12-09, 18:39

Join Date: Jun 2003
Posts: 4,325
Location: Cheshire, UK
grahamhughes's Avatar
grahamhughes
Offline
Forum Nokia Champion
Ahhh... you have spaces in your path name.

You can't write:

Code:
javac -classpath .;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar grabadora.java
If there are spaces, it has to have quotes around the entire classpath, like:

Code:
javac -classpath ".;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar" grabadora.java
I suggest you avoid installing the WTK in a folder with spaces in the path name.

Also, you need all the JARs for all the APIs, so you need the CLDC and MIDP JARs too.
Reply With Quote

#12 Old Re: "public class" is not abstract and does not override abstract method commandActio - 2009-01-15, 07:15

Join Date: Feb 2008
Posts: 1
fgleich
Offline
Registered User
It's complaining because you have declared but failed to override one of the abstract methods. I'm not that familiar with wtk, but there are several inherent commands, like OK, EXIT, and so on. If you declare them as commands, you also must override them somewhere in the code. That is all this error is about.

For instance by declaring "static final Command ExitCommand = new Command("Exit", Command.EXIT, 0);"

it must have a method such like:

public void commandAction(Command c, Displayable s)
{
if (c.getCommandType( ) == Command.EXIT)
{
destroyApp(true);
notifyDestroyed( );
}
}

or you will get that exact same error message.
Reply With Quote
Reply « Previous Thread | Next Thread »
Display Modes
Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules

You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Forum Jump

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia