这是我的应用程序中的fourthpage
类。我收到了错误
遗憾的是,申请已停止工作
我不知道为什么我会这样做。
package student.a.a;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import java.text.DecimalFormat;
public class fourthpage extends Activity {
EditText txtb1, txtb2, txtb3, txtb4, txtb5;
Button btnper;
TextView lblgrade, lblper;
DecimalFormat df = new DecimalFormat("##.##");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourthxml);
// text boxes
txtb1 = (EditText) findViewById(R.id.txtb1);
txtb2 = (EditText) findViewById(R.id.txtb2);
txtb3 = (EditText) findViewById(R.id.txtb3);
txtb4 = (EditText) findViewById(R.id.txtb4);
txtb5 = (EditText) findViewById(R.id.txtb5);
// buttons
btnper = (Button) findViewById(R.id.btnper);
// labels/text view
lblgrade = (TextView) findViewById(R.id.lblgrade);
lblper = (TextView) findViewById(R.id.lblgrade);
}
String tb1 = txtb1.getText().toString();
String tb2 = txtb2.getText().toString();
String tb3 = txtb3.getText().toString();
String tb4 = txtb4.getText().toString();
String tb5 = txtb5.getText().toString();
double txb1 = Double.parseDouble(tb1);
double txb2 = Double.parseDouble(tb2);
double txb3 = Double.parseDouble(tb3);
double txb4 = Double.parseDouble(tb4);
double txb5 = Double.parseDouble(tb5);
double sum = 0;
double total = 0;
public void onClickper(View v) {
sum = txb1 + txb2 + txb3 + txb4 + txb5;
total = (sum / 500) * 100;
String tot = df.format(total);
lblper.setText(tot);
if (total <= 100) {
lblgrade.setText("Congratulations !! , Grade 'A+' ");
} else if (total <= 80) {
lblgrade.setText("Congratulations !! , Grade 'A'");
} else if (total <= 70) {
lblgrade.setText("Grade 'B'");
} else if (total <= 60) {
lblgrade.setText("Grade 'C'");
} else
lblgrade.setText("Failed");
}
}
这是fourthpage
的xml代码。它包含5个文本框,一个按钮和两个textview:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="@+id/tableLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView1"
android:layout_gravity="left"
android:text="Calculate your marks"
android:textAppearance="?android:attr/textAppearanceMedium" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow7"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Space android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_gravity="left"
android:text="Subject 1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtb1"
android:layout_width="162dp"
android:layout_gravity="fill_horizontal" >
<requestFocus />
</EditText>
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/GridLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView01"
android:layout_gravity="left"
android:text="Subject 2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtb2"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/GridLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView02"
android:layout_gravity="left"
android:text="Subject 3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtb3"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/GridLayout03"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView03"
android:layout_gravity="left"
android:text="Subject 4"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtb4"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/GridLayout04"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/TextView04"
android:layout_gravity="left"
android:text="Subject 5"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/txtb5"
android:layout_gravity="fill_horizontal" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow8"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="@+id/btnper"
android:layout_gravity="left"
android:text="Button" android:onClick="onClickper"/>
<Button
android:id="@+id/btngrade"
android:layout_gravity="left"
android:text="Button" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow9"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_gravity="left"
android:text="percentage is "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/lblper"
android:layout_gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</TableRow>
<TableRow
android:id="@+id/tableRow10"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<GridLayout
android:id="@+id/gridLayout6"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView5"
android:layout_gravity="left"
android:text=" Grade is "
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/lblgrade"
android:layout_gravity="left"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</GridLayout>
</TableRow>
</TableLayout>
</LinearLayout>
这是androidmanifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="student.a.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="14" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".StudentsinfoActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".secondpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".thirdpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".fourthpage" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
这是我的logcat:
08-09 16:32:00.970: W/dalvikvm(892): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
08-09 16:32:00.980: E/AndroidRuntime(892): FATAL EXCEPTION: main
08-09 16:32:00.980: E/AndroidRuntime(892): java.lang.RuntimeException: Unable to start activity ComponentInfo{student.a.a/student.a.a.fourthpage}: java.lang.NumberFormatException: Invalid double: ""
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.access$600(ActivityThread.java:122)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.os.Handler.dispatchMessage(Handler.java:99)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.os.Looper.loop(Looper.java:137)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.main(ActivityThread.java:4340)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.reflect.Method.invokeNative(Native Method)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.reflect.Method.invoke(Method.java:511)
08-09 16:32:00.980: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-09 16:32:00.980: E/AndroidRuntime(892): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-09 16:32:00.980: E/AndroidRuntime(892): at dalvik.system.NativeStart.main(Native Method)
08-09 16:32:00.980: E/AndroidRuntime(892): Caused by: java.lang.NumberFormatException: Invalid double: ""
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.StringToReal.parseDouble(StringToReal.java:248)
08-09 16:32:00.980: E/AndroidRuntime(892): at java.lang.Double.parseDouble(Double.java:295)
08-09 16:32:00.980: E/AndroidRuntime(892): at student.a.a.fourthpage.onCreate(fourthpage.java:61)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.Activity.performCreate(Activity.java:4465)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-09 16:32:00.980: E/AndroidRuntime(892): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
08-09 16:32:00.980: E/AndroidRuntime(892): ... 11 more
答案 0 :(得分:0)
仅仅因为您在onCreate
之后放置了变量声明,这并不意味着此代码将在onCreate
之后执行。在调用String
之前和onCreate
成员初始化之前,您的所有EditText
成员都将被初始化。您的double
成员也一样。
String tb1 = txtb1.getText().toString();
String tb2 = txtb2.getText().toString();
String tb3 = txtb3.getText().toString();
String tb4 = txtb4.getText().toString();
String tb5 = txtb5.getText().toString();
double txb1= Double.parseDouble(tb1);
double txb2= Double.parseDouble(tb2);
double txb3= Double.parseDouble(tb3);
double txb4= Double.parseDouble(tb4);
double txb5= Double.parseDouble(tb5);
您必须将初始化置于onCreate
之上,并分别声明String
和double
成员。
...
String tb1, tb2, tb3, tb4, tb5;
double txb1, txb2, txb, txb4, txb5;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fourthxml);
//text boxes
txtb1=(EditText)findViewById(R.id.txtb1);
txtb2=(EditText)findViewById(R.id.txtb2);
txtb3=(EditText)findViewById(R.id.txtb3);
txtb4=(EditText)findViewById(R.id.txtb4);
txtb5=(EditText)findViewById(R.id.txtb5);
//buttons
btnper=(Button)findViewById(R.id.btnper);
//labels/text view
lblgrade=(TextView)findViewById(R.id.lblgrade);
lblper=(TextView)findViewById(R.id.lblgrade);
tb1 = txtb1.getText().toString();
tb2 = txtb2.getText().toString();
tb3 = txtb3.getText().toString();
tb4 = txtb4.getText().toString();
tb5 = txtb5.getText().toString();
txb1= Double.parseDouble(tb1);
txb2= Double.parseDouble(tb2);
txb3= Double.parseDouble(tb3);
txb4= Double.parseDouble(tb4);
txb5= Double.parseDouble(tb5);
}
您正在获取NumberFormatException
,因为您正在解析空字符串。
如果字符串不包含有效的double值,您可以使用以下方法安全地解析double或将其值设置为某个默认值。
public double getDoubleOrDefault(String value, double defValue)
{
try
{
return Double.parseDouble(value);
}
catch (Exception ex)
{
return defValue;
}
}
txb1 = getDoubleOrDefault(tb1, 0.0);