Football Fans: Download the 2012 Schedule App from Google Play!


Go Back   Android Forums > Android Development > Application Development > Developer 101

Developer 101 101 Tutorials



Reply
 
LinkBack Thread Tools
Old February 5th, 2012, 06:46 AM   #1 (permalink)
New Member
 
Join Date: Feb 2012
Posts: 3
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default Gradeaverage app will not start in emulator!

Im trying to make this app that calculates your grade average (based on the Swedish grade system) and I have made the graphical layout and written the java code in eclipse that I think is right, but when I try to launch it in the emulator it won´t work. I just get the following messege. "The application Gradeaverage (process com.dlol.gradeaverage) has stopped unexpectedly. please try again. This is my first app and my first programming project so I don´t really know what I have done wrong.

It is sopposed to be three edittexts where you put the amount of points you have with a cerrtain grade (we have three grades) and then it should multiply the points with the 10, 15 or 20 depending on what grade it is and then divide by the total amount of points. I dont know if that makes sense, but the maxmimum grade is supposed to be 20 (all mvgs). I made a similar app in C# and it works, but Im guessing things are done alittle diferent when it comes to android and java.

Here is the Java code:

Code:
 
package com.dlol.gradeaverage;
 
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
 
 
public class GradeaverageActivity extends Activity {
 
EditText editText1, editText2, editText3;
String gtext, vgtext, mvgtext, str;
Double gpoäng, vgpoäng, mvgpoäng;
Double gvärde, vgvärde, mvgvärde;
Double allapoäng, allavärde;
Double snittbetyg;
Button button1;
 
 
 
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main); 
 
editText1 = (EditText)findViewById(R.id.editText1); 
editText2 = (EditText)findViewById(R.id.editText2); 
editText3 = (EditText)findViewById(R.id.editText3); 
 
gtext = editText1.getText().toString();
vgtext = editText2.getText().toString();
mvgtext = editText3.getText().toString();
 
gpoäng = Double.parseDouble(gtext);
vgpoäng = Double.parseDouble(vgtext);
mvgpoäng = Double.parseDouble(mvgtext);
 
gvärde = gpoäng*10;
vgvärde = vgpoäng*15;
mvgvärde = mvgpoäng*20;
 
allapoäng = (gpoäng + vgpoäng + mvgpoäng);
allavärde = (gvärde + vgvärde + mvgvärde);
 
snittbetyg = (allavärde / allapoäng);
str = "Ditt snitt är " + snittbetyg;
 
button1 = (Button)findViewById(R.id.button1); 
 
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Toast.makeText(getBaseContext(), str,
Toast.LENGTH_SHORT).show();
}
});
}}
 
 
Here is the main.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info_text_G" 
android:textSize="16sp">
</TextView>
 
<EditText 
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"> 
</EditText>/
 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info_text_VG" 
android:textSize="16sp" />
 
<EditText 
android:id="@+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"> 
</EditText>
 
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/info_text_MVG" 
android:textSize="16sp"> 
</TextView>
 
<EditText 
android:id="@+id/editText3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="text"> 
</EditText>
 
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/Button" />
 
</LinearLayout>
 
And here is the strings.xml:
 
<?xml version="1.0" encoding="utf-8"?>
<resources>
 
<string name="hello">Hello World, GradeaverageActivity!</string>
<string name="app_name">Gradeaverage</string>
<string name="info_text_G">Skriv in antal G-poäng</string>
<string name="info_text_VG">Skriv in antal VG-poäng</string>
<string name="info_text_MVG">Skriv in antal MVG-poäng</string>
<string name="Button">Räkna ut snitt!</string>
</resources>

Thanks in advance!

dlol is offline  
Last edited by El Presidente; February 5th, 2012 at 12:56 PM. Reason: Added code tags
Reply With Quote
Sponsors
Old February 5th, 2012, 11:53 AM   #2 (permalink)
Senior Member
 
Join Date: Jul 2010
Posts: 977
 
Device(s): Samsung Galaxy S II, HTC Evo 4G, Amazon Kindle Fire
Thanks: 52
Thanked 199 Times in 144 Posts
Default

Please put your code in CODE tags. It is extremely difficult to read in this form.

When you say "it won't start" what exactly do you mean? Is it throwing an exception on start up? Is it simply starting, then immediately returning to the homescreen? What?
jonbonazza is online now  
Reply With Quote
Old February 5th, 2012, 03:33 PM   #3 (permalink)
Premium Member
 
Join Date: Oct 2011
Location: Sydney, Australia
Posts: 193
 
Device(s): Galaxy Nexus GSM
Thanks: 2
Thanked 37 Times in 33 Posts
Default

You're doing all the average calculation too early in the activity lifecycle.

As your code currently stands, when the activity is created your getting the text in the EditTexts and calculating the average, even before the user has had a chance to enter anything into the EditTexts.

You need to move the code from gtext = editText1.getText().toString(); to str = "Ditt snitt är " + snittbetyg; into the OnClickListener added to button1, so that the code is run when the user clicks the button and not when the activity is created.

Go back and look carefully at where you did this in your C# app. You would have do it in a similar place.
jiminaus is offline  
Reply With Quote
The Following User Says Thank You to jiminaus For This Useful Post:
dlol (February 5th, 2012)
Old February 5th, 2012, 04:44 PM   #4 (permalink)
New Member
 
Join Date: Feb 2012
Posts: 3
 
Device(s):
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I´m sorry about the code tags, but thanks to you who edited it for me^^

And yeah jiminaus you were right, I moved the code around and now it works! Thanks alot for the help
dlol is offline  
Reply With Quote
Reply

Bookmarks


Go Back   Android Forums > Android Development > Application Development > Developer 101 User CP
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -5. The time now is 08:26 PM.
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
Custom vBulletin Skins by: Relivo