I am 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. The app starts, but its only a black screen. This is my first app and my first programming project so I don´t really know what I have done wrong.
It is supposed 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 a little different when it comes to android and java.
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();
}
});
}}
<?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>
<?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>
02-05 17:07:06.184: D/AndroidRuntime(1230): Shutting down VM
02-05 17:07:06.184: W/dalvikvm(1230): threadid=3: thread exiting with uncaught exception (group=0x4000fe70)
02-05 17:07:06.184: E/AndroidRuntime(1230): Uncaught handler: thread main exiting due to uncaught exception
02-05 17:07:06.194: E/AndroidRuntime(1230): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dlol.gradeaverage/com.dlol.gradeaverage.GradeaverageActivity}: java.lang.NumberFormatException:
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2268)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2284)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread.access$1800(ActivityThread.java:112)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1692)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.os.Handler.dispatchMessage(Handler.java:99)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.os.Looper.loop(Looper.java:123)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread.main(ActivityThread.java:3948)
02-05 17:07:06.194: E/AndroidRuntime(1230): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 17:07:06.194: E/AndroidRuntime(1230): at java.lang.reflect.Method.invoke(Method.java:521)
02-05 17:07:06.194: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:782)
02-05 17:07:06.194: E/AndroidRuntime(1230): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:540)
02-05 17:07:06.194: E/AndroidRuntime(1230): at dalvik.system.NativeStart.main(Native Method)
02-05 17:07:06.194: E/AndroidRuntime(1230): Caused by: java.lang.NumberFormatException:
02-05 17:07:06.194: E/AndroidRuntime(1230): at org.apache.harmony.luni.util.FloatingPointParser.parseDouble(FloatingPointParser.java:258)
02-05 17:07:06.194: E/AndroidRuntime(1230): at java.lang.Double.parseDouble(Double.java:323)
02-05 17:07:06.194: E/AndroidRuntime(1230): at com.dlol.gradeaverage.GradeaverageActivity.onCreate(GradeaverageActivity.java:37)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
02-05 17:07:06.194: E/AndroidRuntime(1230): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
02-05 17:07:06.194: E/AndroidRuntime(1230): … 11 more
02-05 17:07:06.225: I/dalvikvm(1230): threadid=7: reacting to signal 3
02-05 17:07:06.244: I/dalvikvm(1230): Wrote stack trace to ‘/data/anr/traces.txt’
02-05 17:07:06.344: I/jdwp(1230): received file descriptor 24 from ADB
02-05 17:07:06.354: W/System.err(1230): Can’t dispatch DDM chunk 46454154: no handler defined
02-05 17:07:06.354: W/System.err(1230): Can’t dispatch DDM chunk 4d505251: no handler defined
You get a
NumberFormatExceptionbecause when you create theActivity(onCreate()method) you already try to parse the text from theEditText(you have emty text""and trying to parse it toDoublewill trow anException) although you didn’t entered any numbers yet. You have to move the code for calculating the grades on the button’sOnCLickListenerlike this: