BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 05-10-2010, 10:44 AM   #1 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Unhappy Need some help with buttons

Please Login to Remove!

Hello,,,

I am required to develop a software that calculates student major gpa and cumulative gpa. I have written the code for the first screen that user has to choose which type of gpa he want to calculate by clicking on the button, the image in attachments illustrates that.

However, I don't know how to write the code for when the user clicks on a button using trackball/trackwheel and it pushes him to another screen which does the required function..

Can I get some help please....

I'm supposed to submit it by the end of the week
Attached Images
File Type: png 9700.png (14.7 KB, 11 views)
Offline  
Old 05-10-2010, 10:54 AM   #2 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

You should extend FieldChangeListener as a private class in the screen class that holds these buttons.

Make your own class:

class ButtonListener extends FieldChangeListener

In the "fieldChanged" method (which you must override), you will look for the particular button and perform the appropriate operation.

Don't forget to place the style CONSUME_CLICK on your button fields (if you have not already done so).
Offline  
Old 05-10-2010, 11:47 AM   #3 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Errmmmm.. I did not understand how am I supposed to that

This is my code so far..

Code:
package com.rim.samples.GPAcal;


import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import java.lang.*;

public class GPAcal extends UiApplication{
	public static void main (String[] args)
	{
		GPAcal app= new GPAcal();
		 app.enterEventDispatcher();
		
	}
	public GPAcal()
	{
		pushScreen(new GPAcalScreen());
	}
}


final class GPAcalScreen extends MainScreen 
{
	private ButtonField sms;
	private ButtonField cum;
	private ButtonField major;
	private SemGPAScreen smstr;
	private Object SemGPAScreen;
	
	public GPAcalScreen()
	{
	super();
	LabelField title = new LabelField("GPA Calculator",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		setTitle(title);

		add(new RichTextField("Choose type of GPA you want to calculate:"));
		
		sms= new ButtonField ("Semester GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER);
		cum= new ButtonField ("Cumulative GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER);
		major= new ButtonField ("Major GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER);
	
		add(sms);
		add(major);
		add(cum);
		
	}
Offline  
Old 05-10-2010, 01:27 PM   #4 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I added the class ButtonListener in the screen class as u said,, but how do i push the "SemGPAScreen" to the main screen??
Thought i created a " _SemGPAScreen" which is a private object of type SemGPAScreen in the screen class.
The red line is the error line.. any idea about how to fix it?

class ButtonListener implements FieldChangeListener
{
public void fieldChanged(Field field, int context)
{
ButtonField btn = (ButtonField) field;
if(btn.getLabel()=="Semester GPA")
UiApplication.getUiApplication().pushScreen.(_SemG PAScreen);
}
}

Last edited by miz_g6 : 05-10-2010 at 01:29 PM.
Offline  
Old 05-10-2010, 03:34 PM   #5 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

You cannot compare two string objects using '=='. You use string.equals(anotherString) (see the javadocs).

I would use something like this:

if (field == sms) {
...do something
}

Of course, you'll have to make these buttonfields "public" to do this.

Also, you have a reference to your UiApplication, why are you calling UiApplication.getUiApplication()?

Also, you need to isntantiate the screen that you push, like pushScreen(new SemGPaScreen())

My advice: get yourself a java book and work through the JDE samples.

Last edited by Dougsg38p : 05-10-2010 at 03:36 PM.
Offline  
Old 05-14-2010, 08:13 AM   #6 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

i tried all that,, and every other possiblity but i still can't make it push the screeen.. i hv done some modification but I still get error if i pushed the screen without using UiApplication.getUiApplication().. :(.. I'm a nooooob and 'm so confused !!!

Code:
package com.rim.samples.GPAcal;


import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.system.*;
import net.rim.device.api.i18n.*;
import java.lang.*;

import com.rim.samples.GPAcal.GPAcalScreen.SemGPAScreen;

public class GPAcal extends UiApplication{
	public static void main (String[] args)
	{
		GPAcal app= new GPAcal();
		 app.enterEventDispatcher();
		
	}
	public GPAcal()
	{
		pushScreen(new GPAcalScreen());
	}
}


final class GPAcalScreen extends MainScreen implements FieldChangeListener
{
	public ButtonField sms;
	public ButtonField cum;
	public ButtonField major;
	
	
	
	public GPAcalScreen()
	{
	super();
	LabelField title = new LabelField("GPA Calculator",
            LabelField.ELLIPSIS | LabelField.USE_ALL_WIDTH);
		setTitle(title);

		add(new RichTextField("Choose type of GPA you want to calculate:"));
	
		sms= new ButtonField ("Semester GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER|ButtonField.CONSUME_CLICK);
		sms.setChangeListener(this);
		cum= new ButtonField ("Cumulative GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER|ButtonField.CONSUME_CLICK);
		cum.setChangeListener(this);
		major= new ButtonField ("Major GPA",ButtonField.FIELD_HCENTER|ButtonField.FIELD_VCENTER|ButtonField.VCENTER|ButtonField.HCENTER|ButtonField.CONSUME_CLICK);
		major.setChangeListener(this);
		add(sms);
		add(major);
		add(cum);
		
	}

		public void fieldChanged(Field field, int context)
			{
				
				if(field==sms)
				pushScreen(new  SemGPAScreen());
			}
				
		
		// when Exiting the software
		public boolean onClose()
	      {
	             System.exit(0);
	              return true;
	      }


	
	// Semester GPA screen	
	  class SemGPAScreen extends MainScreen 
	{
		private BasicEditField sub[];
		private ObjectChoiceField crd[];
		private ObjectChoiceField grd[];
		String[] credits={"3","4"};
		String[] grades={"A","A-","B+","B","B-","C+","C","C-","D+","D","F"};
		String totC;
		String cumG; 
		float gr=0;
		float cr=0;
		float total=0;
		
		public SemGPAScreen()
		{
			super();
			add(new RichTextField("Subject: "));
			add(new RichTextField("Credits: "));
			add(new RichTextField("Grades: "));
			for (int i=0; i<7; i++)
			{
				sub[i]= new BasicEditField(Field.EDITABLE);
				add(sub[i]);
				crd[i]= new ObjectChoiceField("", credits);
				add(crd[i]);
				grd[i]= new ObjectChoiceField("", grades);
				add(grd[i]);
			}
			BasicEditField cumg= new BasicEditField("Current GPA:", cumG, 4,Field.EDITABLE);
			BasicEditField tot= new BasicEditField("Credits Passed:", totC,3,Field.EDITABLE);
			add(cumg);
			add(tot);
			
			float cg= Float.parseFloat(cumG);
			float tt= Float.parseFloat(totC);
			for (int j=0; j<7; j++)
			{
				
				if(crd[j].getSelectedIndex()==0)
				{
					cr+=3;
				gr=Cal(j);
				total+=cr*gr;
				}
				
				else
				{		
					cr+=4;
					gr=Cal(j);
					total+=cr*gr;
					
				}
			}
			float semesterGPA= (total+cg)/(cr+tt);
			Dialog.alert(" Your GPA for this semester will be"+semesterGPA);
		}
			
			public float Cal(int k)
			{
					if(grd[k].getSelectedIndex()==0)
					{
						gr+=4.00;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==1)
					{
						gr+=3.67;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==2)
					{
						gr+=3.33;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==3)
					{
						gr+=3.00;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==4)
					{
						gr+=2.67;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==5)
					{
						gr+=2.33;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==6)
					{
						gr+=2.00;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==7)
					{
						gr+=1.67;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==8)
					{
						gr+=1.33;
						return gr;
					}
					else if (grd[k].getSelectedIndex()==9)
					{
						gr+=1.00;
						return gr;
					}
					else
					{
						gr+=0.00;
						return gr;
					}
				}
					
				
			
	}
	
	
}

Last edited by miz_g6 : 05-14-2010 at 08:16 AM.
Offline  
Old 05-14-2010, 08:46 AM   #7 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

Pull the class SemGPAScreen out into a separate file called SemGPAScreen.java

Change the declaration as follows:

public class SemGPAScreen extends MainScreen

...and please get yourself a Java book
Offline  
Old 05-14-2010, 09:32 AM   #8 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

I copied your code and got it running.

You can leave the two local classes in the main file, but I would consider it poor practice. My advice is to pull the SemGPAScreen and GPAcalScree classes into separate files (of the same name).

The issue with pushScreen is that you are calling it from a Screen class, not a UiApplicaiton class. pushScreen() is not a method of Screen, it is a method of UiApplication. So, I see why you are using UiApplication.getUiApplication().pushScreen(). Not the way I would have done it, but this is acceptable. I gave you some bad (or incomplete) advice in the earlier post.
Offline  
Old 05-15-2010, 03:51 AM   #9 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did exatly what you said.. I pulled class GPAcalScreen and SemGPAScreen into separate files with the same class name and changed the declaration into public class.....
But still, it is not pushing the screen when i run it on the simulator!!
Offline  
Old 05-15-2010, 11:52 AM   #10 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

I get your code running. Keep kicking on it.

Are you throwing an exception?
Offline  
Old 05-15-2010, 03:09 PM   #11 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

it's exactly the same code as above, i jst put them separate files.
when i run it on simulator and click on the first button it does not push the screen.. i get :
JVM Error: 104
Uncaught: NullPointer Exeception
Offline  
Old 05-15-2010, 04:12 PM   #12 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

OK, where are you getting the exception? Open the call-stack view.

EDIT: I see your problem.

You declare these arrays:

private BasicEditField sub[];
private ObjectChoiceField crd[];
private ObjectChoiceField grd[];

But you never allocation storage for them.

You need this in your constructor:

sub = new BasicEditField[<whatever size you want>]

...etc

Did you get that Java book?

Last edited by Dougsg38p : 05-15-2010 at 04:16 PM.
Offline  
Old 05-15-2010, 04:46 PM   #13 (permalink)
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Errrrrmmmm.. tried it,, but still the same error!!
Offline  
Old 05-15-2010, 11:21 PM   #14 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

Dude...you REALLY need to learn to use the tools.

Open your call stack view. You are getting a DIFFERENT NPE - this time on cumG:

float cg= Float.parseFloat(cumG);
float tt= Float.parseFloat(totC);

Once again, you have declared cumG as a String, but failed to put anyting in it, so it is null. This is what NULL POINTER means. Bascially, you are trying to initialize the edit field with a null value.

And after you fix that one, you also have another null value in totC.

If you do this:

String foobar;

...foobar is null - it has no value. Trying to access it will create a null pointer exception.

You can do this:

String foobar;

foobar = "123";

..or this:

String foobar = "123";

Last edited by Dougsg38p : 05-15-2010 at 11:25 PM.
Offline  
Old 05-15-2010, 11:45 PM   #15 (permalink)
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Post Thanks: 0
Thanked 34 Times in 33 Posts
Default

...and here is another issue:

The code the calculates the GPA is in the constructor of the SemGPAScreen class. This is never going to work. You need to understand that this is EVENT DRIVEN programming, not linear/procedural programming.

You are adding some fields to the screen in the constructor (correct), then attempting to retrieve the values from the fields in the same constuctor (incorrect). The fields will not even be displayed to the user until you exit the constructor and allow the framework to display the screen.

What you need to do here is probably have a button on this screen "Submit" (or something like that). You place a listener on the button, then perform the calculations and display the results in another screen (or in a text field that you add to this screen).


What is happening with your code is that the calcs are performed on null values (yielding no useful data), then you try to put up a dialog (which will fail with an IllegalState exception, since you have not even entered the event thread when this Dialog is called).

Going back to my original advice: get yourself a book (there are several beginner BB Java books available now), and work through the examples. There is nothing in what you are attempting that is very complicated - your difficulties lie in the fact that you do not understand the basic principles.
Offline  
Closed Thread


Thread Tools

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 On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.