BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 05-10-2010, 09:44 AM   #1
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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, 09:54 AM   #2
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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, 10:47 AM   #3
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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, 12:27 PM   #4
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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 12:29 PM..
Offline  
Old 05-10-2010, 02:34 PM   #5
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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 02:36 PM..
Offline  
Old 05-14-2010, 07:13 AM   #6
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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 07:16 AM..
Offline  
Old 05-14-2010, 07:46 AM   #7
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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, 08:32 AM   #8
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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, 02:51 AM   #9
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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, 10:52 AM   #10
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
Default

I get your code running. Keep kicking on it.

Are you throwing an exception?
Offline  
Old 05-15-2010, 02:09 PM   #11
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
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, 03:12 PM   #12
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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 03:16 PM..
Offline  
Old 05-15-2010, 03:46 PM   #13
miz_g6
New Member
 
Join Date: May 2010
Model: 9700
PIN: N/A
Carrier: Zain (Vodafone)
Posts: 8
Default

Errrrrmmmm.. tried it,, but still the same error!!
Offline  
Old 05-15-2010, 10:21 PM   #14
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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 10:25 PM..
Offline  
Old 05-15-2010, 10:45 PM   #15
Dougsg38p
BlackBerry Extraordinaire
 
Join Date: Mar 2008
Location: Austin, TX
Model: 9700
PIN: N/A
Carrier: T-Mobile
Posts: 1,644
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



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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


128K RAM - APPLE - ORIGINAL APPLE prototype BOARD picture

128K RAM - APPLE - ORIGINAL APPLE prototype BOARD

$408.75



APPLE 630-0895-B  VRAM 128K X 8 BOARD CARD VINTAGE picture

APPLE 630-0895-B VRAM 128K X 8 BOARD CARD VINTAGE

$74.77



APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD  picture

APPLE 820-0522-A 630-0895-B LITE VRAM 128K X 8 BOARD

$149.99







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