我制作的应用用户在两个文本字段中输入两个数字,然后按"添加"按钮和应用程序生成一个显示两个数字之和的吐司。该应用程序运行,但当我按下"添加"按钮,它说"不幸的是,ToastSum已停止工作"。我做错了什么?
这是xml文件:(activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:hint="number 1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/n1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:hint="number 2"
android:ems="10" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/n2"
android:layout_centerHorizontal="true"
android:layout_marginTop="48dp"
android:text="Add" />
</RelativeLayout>
这是Java代码:(MainActivity.java)
package com.example.toastsum;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
@SuppressWarnings("deprecation")
public class MainActivity extends ActionBarActivity {
Button add;
EditText t1,t2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add=(Button) findViewById(R.id.b1);
t1=(EditText) findViewById(R.id.n1);
t2=(EditText) findViewById(R.id.n2);
add.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
while(t1.toString().equals(null) || t2.toString().equals(null))
{
Toast.makeText(MainActivity.this, "Enter two numbers", Toast.LENGTH_SHORT).show();
}
double d1,d2;
d1=Double.parseDouble(t1.toString());
d2=Double.parseDouble(t2.toString());
d1+=d2;
String s=String.valueOf(d1);
Toast.makeText(MainActivity.this,s, Toast.LENGTH_LONG).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.toastsum"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
这是Logcat:
06-22 12:11:11.789: E/AndroidRuntime(1225): FATAL EXCEPTION: main
06-22 12:11:11.789: E/AndroidRuntime(1225): Process: com.example.toastsum, PID: 1225
06-22 12:11:11.789: E/AndroidRuntime(1225): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.toastsum/com.example.toastsum.MainActivity}: java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{10b5c225 VFED..CL .F....I. 0,0-0,0 #7f09004f app:id/n1}"
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread.access$800(ActivityThread.java:151)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.os.Handler.dispatchMessage(Handler.java:102)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.os.Looper.loop(Looper.java:135)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:11:11.789: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:11:11.789: E/AndroidRuntime(1225): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:11:11.789: E/AndroidRuntime(1225): Caused by: java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{10b5c225 VFED..CL .F....I. 0,0-0,0 #7f09004f app:id/n1}"
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:11:11.789: E/AndroidRuntime(1225): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:11:11.789: E/AndroidRuntime(1225): at com.example.toastsum.MainActivity.onCreate(MainActivity.java:27)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.Activity.performCreate(Activity.java:5990)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
06-22 12:11:11.789: E/AndroidRuntime(1225): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
06-22 12:11:11.789: E/AndroidRuntime(1225): ... 10 more
06-22 12:13:45.489: E/AndroidRuntime(1278): FATAL EXCEPTION: main
06-22 12:13:45.489: E/AndroidRuntime(1278): Process: com.example.toastsum, PID: 1278
06-22 12:13:45.489: E/AndroidRuntime(1278): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{3850235a VFED..CL ........ 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:13:45.489: E/AndroidRuntime(1278): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.view.View.performClick(View.java:4780)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:135)
06-22 12:13:45.489: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:13:45.489: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:13:45.489: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:13:45.489: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:16:15.820: E/AndroidRuntime(1314): FATAL EXCEPTION: main
06-22 12:16:15.820: E/AndroidRuntime(1314): Process: com.example.toastsum, PID: 1314
06-22 12:16:15.820: E/AndroidRuntime(1314): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{6ee1150 VFED..CL .F...... 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:16:15.820: E/AndroidRuntime(1314): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.view.View.performClick(View.java:4780)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.os.Looper.loop(Looper.java:135)
06-22 12:16:15.820: E/AndroidRuntime(1314): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:16:15.820: E/AndroidRuntime(1314): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:16:15.820: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:16:15.820: E/AndroidRuntime(1314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:18:47.286: E/AndroidRuntime(1338): FATAL EXCEPTION: main
06-22 12:18:47.286: E/AndroidRuntime(1338): Process: com.example.toastsum, PID: 1338
06-22 12:18:47.286: E/AndroidRuntime(1338): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{9d1d602 VFED..CL .F...... 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:18:47.286: E/AndroidRuntime(1338): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.view.View.performClick(View.java:4780)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.os.Looper.loop(Looper.java:135)
06-22 12:18:47.286: E/AndroidRuntime(1338): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:18:47.286: E/AndroidRuntime(1338): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:18:47.286: E/AndroidRuntime(1338): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:18:47.286: E/AndroidRuntime(1338): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:24:46.435: E/AndroidRuntime(1392): FATAL EXCEPTION: main
06-22 12:24:46.435: E/AndroidRuntime(1392): Process: com.example.toastsum, PID: 1392
06-22 12:24:46.435: E/AndroidRuntime(1392): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{9d1d602 VFED..CL .F....ID 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:24:46.435: E/AndroidRuntime(1392): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.view.View.performClick(View.java:4780)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.os.Looper.loop(Looper.java:135)
06-22 12:24:46.435: E/AndroidRuntime(1392): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:24:46.435: E/AndroidRuntime(1392): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:24:46.435: E/AndroidRuntime(1392): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:24:46.435: E/AndroidRuntime(1392): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:25:10.723: E/AndroidRuntime(1415): FATAL EXCEPTION: main
06-22 12:25:10.723: E/AndroidRuntime(1415): Process: com.example.toastsum, PID: 1415
06-22 12:25:10.723: E/AndroidRuntime(1415): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{3850235a VFED..CL ........ 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:25:10.723: E/AndroidRuntime(1415): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.view.View.performClick(View.java:4780)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.os.Looper.loop(Looper.java:135)
06-22 12:25:10.723: E/AndroidRuntime(1415): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:25:10.723: E/AndroidRuntime(1415): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:25:10.723: E/AndroidRuntime(1415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:25:10.723: E/AndroidRuntime(1415): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
06-22 12:28:58.352: E/AndroidRuntime(1440): FATAL EXCEPTION: main
06-22 12:28:58.352: E/AndroidRuntime(1440): Process: com.example.toastsum, PID: 1440
06-22 12:28:58.352: E/AndroidRuntime(1440): java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText{9d1d602 VFED..CL .F...... 80,84-400,152 #7f09004f app:id/n1}"
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.StringToReal.invalidReal(StringToReal.java:63)
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.StringToReal.initialParse(StringToReal.java:114)
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.StringToReal.parseDouble(StringToReal.java:282)
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.Double.parseDouble(Double.java:301)
06-22 12:28:58.352: E/AndroidRuntime(1440): at com.example.toastsum.MainActivity$1.onClick(MainActivity.java:34)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.view.View.performClick(View.java:4780)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.view.View$PerformClick.run(View.java:19866)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.os.Handler.handleCallback(Handler.java:739)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.os.Handler.dispatchMessage(Handler.java:95)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.os.Looper.loop(Looper.java:135)
06-22 12:28:58.352: E/AndroidRuntime(1440): at android.app.ActivityThread.main(ActivityThread.java:5257)
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.reflect.Method.invoke(Native Method)
06-22 12:28:58.352: E/AndroidRuntime(1440): at java.lang.reflect.Method.invoke(Method.java:372)
06-22 12:28:58.352: E/AndroidRuntime(1440): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
06-22 12:28:58.352: E/AndroidRuntime(1440): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:2)
请将t1.toString()
替换为t1.getText.toString();
答案 1 :(得分:1)
您的EditText
之一不包含有效的双精度值,从而导致
d1=Double.parseDouble(t1.toString());
d2=Double.parseDouble(t2.toString());
失败。
此检查
while(t1.toString().equals(null) || t2.toString().equals(null)) {
Toast.makeText(MainActivity.this, "Enter two numbers", Toast.LENGTH_SHORT).show();
}
错了。它应该是
if (TextUtils.isEmpty(t1.getText().toString()) || TextUtils.isEmpty(t2.getText().toString()) {
Toast.makeText(MainActivity.this, "Enter two numbers", Toast.LENGTH_SHORT).show();
return;
}
答案 2 :(得分:1)
这是错误的。您正在尝试将EditText转换为字符串并将其解析为double。
d1=Double.parseDouble(t1.toString());
d2=Double.parseDouble(t2.toString());
你可能想要
d1 = Double.parseDouble(t1.getText().toString());
哦,你的while循环看起来很怪异。您可能需要if检查,如果文本无效则返回