views:

1400

answers:

3

I'm sort of confused as to how to implement a FieldChangeListener in the Blackberry JDE. One way has me make my main class implement FieldChangeListener, and then have a fieldchanged method inside of it, and another has me do:

    FieldChangeListener listenerUS = new FieldChangeListener() {
public void fieldChanged(Field field, int context) {
System.out.println("Something changed!");
pushScreen(_newScreen);
}
};

Either way, if I try to call a method (like pushScreen, or a custom method I've written), I get a runtime error. In debug mode, none of my print statements are being displayed, either. However, if I remove the fieldChanged method outright, it won't even compile, so I"m pretty sure it's seeing the code?

I've added the listener to the button I want it hooked up to either by having:

         but_temp.setChangeListener(this);

(in the first case) or by putting listenerUS.

Everything seems to be hooked up, but of my print statements show up, and if I call a method, I get a runtime error.

Does this make sense? Am I just completely confused about how to use listeners on the blackberry?

http://pastie.org/618950

There's a copy of my code as a whole...

+1  A: 

I looked at your code and nothing blatantly wrong jumped out at me. However, I wouldn't designate the main application class the duties of being the FieldChangeListener. It's not something it should have to be aware of. The best I can do for you is provide an example app that implements the FieldChangeListener interface for a ButtonField. It's not a solution but maybe with your better knowledge of your code you'll be able to pick something out that is different than this example. Hope it helps.

import net.rim.device.api.ui.Field;
import net.rim.device.api.ui.UiApplication;
import net.rim.device.api.ui.container.MainScreen;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.component.LabelField;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.FieldChangeListener;

/**
 * Test implementation of ButtonField.
 */
public class TestAppMain extends UiApplication 
{
    /**
     * Default Constructor.
     */
    private TestAppMain() {        
        pushScreen(new AppScreen());
    }

    /**
     * App entry point.
     * @param args Arguments.
     */
    public static void main(String[] args) {
        TestAppMain app = new TestAppMain();
        app.enterEventDispatcher();
    }

    /**
     * Main application screen.
     */
    private static class AppScreen extends MainScreen 
    {
        /**
         * Default constructor.
         */
        public AppScreen() {
            LabelField title = new LabelField("Button Test Demo",
                    LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
            setTitle(title);

            // Create a button with a field change listener.
            FieldChangeListener listener = new FieldChangeListener() {
                public void fieldChanged(Field field, int context) {
                    ButtonField buttonField = (ButtonField) field;
                    System.out.println("Button pressed: " + buttonField.getLabel());
                }
            };
            ButtonField buttonField = new ButtonField("Test Button", ButtonField.CONSUME_CLICK);            
            buttonField.setChangeListener(listener);
            add(buttonField);
        }               

        /**
         * Handle app closing.
         */
        public void close() {
            Dialog.alert("Goodbye!");
            System.exit(0);
            super.close();
        }
    }
}
Fostah
*nods* Yeah, that's the other way I was doing it. I am however, really confused as to how much goes in the screen itself, vs. methods in the main code. A lot of the tutorials BB provided leave the screen mostly blank. It does make a lot of sense to do things in screen, though.
Jenny
A: 

I agree with Fostah(+1), it's common to implement FieldChangeListener in Field, Manager or Screen, or use a standalone FieldChangeListener.
Also, to push/pull screen from Field:

UiApplication.getUiApplication().pushScreen(nextScreen);

See How to navigate back to the previous screen in the Blackberry emulator?

Max Gontar
Hrrm...trying to do this makes my print statement succesfully print (hooray!), but the program crashes harder. Before, it went to that white error screen with the ""JUM Error 104: Uncaught NullPointer Exception", but nothing would actually kick me out (I could hit 'continue' and keep running my program. NOW, however, it kicks me into the debug perspective in eclipse. If I undo my code changes to get it back to how it was, it's still doing this, which confuses me.
Jenny
calling ANY method within the listener crashes it to the debug screen for "Screen.class", as well. I figure I'll refactor all of my code to make the brunt of the work happen in my providerScreen class, then see what happens.
Jenny
When you find reason and solution, it will be great to have your answer.
Max Gontar
*sigh* I can get the listener to work properly in a new class, with no code in it other than the button and listener. However, if I comment EVErYTHING out but a test button and listener in my original class, it still crashes. It's so confusing.
Jenny
Huh. So, I tried to refactor things, and noticed that the meat of the work was in the Screen class in the example Fostah gave. However, if I try to emulate this, my code crashes on startup, without getting anywhere, saying that there's something wrong with Connection. Can you just not do HTTPConnections from screens?
Jenny
general recommendation is to do networking, file read/write and other heavy stuff in separate thread. check this out: coderholic.com/blackberry-webbitmapfield/
Max Gontar
A: 

I'm super confused, but I managed to fix things. I created a new class from scratch, and then just copied and pasted my old code into it. Everything works. The only thing I changed was only importing classes that Eclipse said was necessary (before I had some import statements from various tutorials, etc. so some were possibly not being used.)

Is it possible I was importing something that was causing things to crash?

I really would rather have most of my code in the screen itself, but trying that crashes the whole thing before I can even load. Something about the xml parser I'm using not being happy.

http://pastie.org/621932

There's the modified code. I'm really frustrated, because I know that there is some inherent UNDERSTANDING of this frame work that I'm not grokking, and that most of my troubles are coming from this. I suppose only practice will help me, though ^_^;;

Jenny