| Reply | « Previous Thread | Next Thread » |
|
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.
|
|
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? |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
Join Date: Mar 2008
Posts: 2,161
Location: The Capital of I N D I A
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:
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 |
|
raj... he knows that... see the line:
Code:
public void commandAction(Command c, Displayable s) {
|
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
Quote:
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 |
|
Join Date: Mar 2008
Posts: 2,161
Location: The Capital of I N D I A
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 |
|
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? |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
Quote:
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 |
|
No problem... quick Java lesson...
A fully qualified class name ("FQN") is like: Code:
javax.microedition.lcdui.Command 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 Code:
javax\microedition\lcdui\Command.class 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) {
}
}
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 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? |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
Quote:
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); } } |
|
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 Code:
javac -classpath ".;c:\Program Files\NetBeans 6.1\mobility8\WTK2.5.2\lib\jsr75.jar" grabadora.java Also, you need all the JARs for all the APIs, so you need the CLDC and MIDP JARs too. |
| grahamhughes |
| View Public Profile |
| Find all posts by grahamhughes |
|
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 | « Previous Thread | Next Thread » |
| Thread Tools | Search this Thread |
|---|---|