Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a
Accepted Solution

integer passing to another class problem, please help :D

I've been trying for a day now to fix this and I'm stumped. Could someone please point out what is wrong with the following code? I'm trying to pass an integer from one class to another.

 

ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER) {
            protected boolean trackwheelClick(int status, int time){
                if (firstChoice == 0) {
                    Dialog.alert("You must choose !");
                                                    }
                    else {
                    int firstChoices = firstChoice;
                    UiApplication.getUiApplication().popScreen(Ui.getUiEngine().getActiveScreen());
                    UiApplication.getUiApplication().pushScreen(new newGame3(firstChoices));
                   
                            }   
                return true;        }           
                public int getPreferredWidth() {
                return Display.getWidth();
                                                }
            protected void paint (Graphics graph){
                graph.setBackgroundColor(Color.BLUE);
                graph.fillRect(0, 0, getWidth(), getHeight());
                graph.setColor(Color.RED);
                graph.clear();
                super.paint(graph);
                                                }
            };
       
        nextBut.setFont(Font.getDefault().derive(Font.BOLD));
       
        FieldChangeListener lnextBut = new FieldChangeListener() {
            public void fieldChanged(Field field, int context) {           
            }
        }
        ;
        nextBut.setChangeListener(lnextBut);
        Vman.add(nextBut);

 

the other class:

 

public class newGame3 extends MainScreen{
   
    public newGame3(int firstChoices){
        LabelField t1 = new LabelField(" New Game 3 "+ firstChoices);
        add(t1);
    }
}

 

 

 

 

 

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: integer passing to another class problem, please help :D

Hi,

 

You don't need to override trackwheelClick() .

FieldChangeListener will do that for you.

So add your button click code inside your fieldChanged() method.

 

Regards

Bikas

 

 

Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: integer passing to another class problem, please help :D

Thanks I'll try that. But that isn't my problem (the button works on all my screens).

 

My problem lies in the integer. It's not passing it through and it causes that class screen to lock up in an error when I run the simulator. Can someone please help with that?

Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: integer passing to another class problem, please help :D

UiApplication.getUiApplication().pushScreen(new newGame3(firstChoices));

 

its underlining the (new NewGame3(firstChoices)); in red but its not giving me a reason to work with

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: integer passing to another class problem, please help :D

What error message are you getting?

 

Regards

Bikas

Developer
RexDoug
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: integer passing to another class problem, please help :D

[ Edited ]

The variable "firstChoices" is neither defined nor set in the button class.

 

My guess is that your error is that firstChoice is either not visible in the scope of the button class, or it it's value has not been set.

 

One more observation: trackWheelClick() is a deprecated method and you should avoid it.

Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: integer passing to another class problem, please help :D

isn't this line defining it and in the button class? 

 

int firstChoices = firstChoice;

 

maybe you could show me an example of what you mean, thanks

 

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: integer passing to another class problem, please help :D

I think Rexdoug meant firstChoice.

 

Give a look at this example. You should get whats wrong on your code.

 

public class TestScreen extends MainScreen
{
    int firstChoice = 5;
    public TestScreen()
    {
        ButtonField nextBut = new ButtonField(" Next ", Field.FIELD_HCENTER | ButtonField.CONSUME_CLICK);
        FieldChangeListener lnextBut = new FieldChangeListener() 
        {
            public void fieldChanged(Field field, int context) 
            {   
                UiApplication.getUiApplication().popScreen(Ui.getUiEngine().getActiveScreen()); 
                UiApplication.getUiApplication().pushScreen(new NewGame3(firstChoice));                
            }
        };
        nextBut.setChangeListener(lnextBut);
        this.add(nextBut);
    }  
} 

class NewGame3 extends MainScreen
    {
        public NewGame3(int firstChoice) 
        {
            LabelField t1 = new LabelField(" New Game 3 "+ firstChoice);
            this.add(t1);    
        }
    }  

Regards

Bikas

Trusted Contributor
QuestionMan
Posts: 167
Registered: ‎06-30-2011
My Device: n/a

Re: integer passing to another class problem, please help :D

When I run the simulator and press the next button on that screen it goes to a JVM Error, Uncaught, Illegal State Exception...oh and nothing is underlined now for some reason. I'm pretty lost on this one.

Developer
bikas
Posts: 984
Registered: ‎02-10-2009
My Device: Not Specified

Re: integer passing to another class problem, please help :D

Hi,

 

I think you are trying to add somthing on the screen that does not belong to that screen or already added from a background Thread.

 

Can you let us know exacttly what are you trying to achieve with code.

Which line of your code causing this error.

 

Regards

Bikas