应用程序在 Android 模拟器中停止工作
Posted
技术标签:
【中文标题】应用程序在 Android 模拟器中停止工作【英文标题】:Application stops working in Android Emulator 【发布时间】:2015-09-07 14:16:03 【问题描述】:我正在制作一个应用程序,其中用户在两个文本字段中输入两个数字,然后按“添加”按钮,应用程序会生成一个 toast,显示两个数字的总和。该应用程序运行,但当我按下“添加”按钮时,它显示“不幸的是,ToastSum 已停止工作”。我做错了什么?
这里是xml文件:(activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_ >
<EditText
android:id="@+id/n1"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_
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 ComponentInfocom.example.toastsum/com.example.toastsum.MainActivity: java.lang.NumberFormatException: Invalid double: "android.support.v7.widget.AppCompatEditText10b5c225 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.AppCompatEditText10b5c225 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.AppCompatEditText3850235a 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.AppCompatEditText6ee1150 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.AppCompatEditText9d1d602 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.AppCompatEditText9d1d602 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.AppCompatEditText3850235a 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.AppCompatEditText9d1d602 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)
【问题讨论】:
【参考方案1】:请将t1.toString()
替换为t1.getText.toString();
【讨论】:
【参考方案2】:您的一个EditText
s 不包含有效的双精度值,这会导致来自
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;
【讨论】:
【参考方案3】:这是错误的。您正在尝试将 EditText 转换为字符串并将其解析为双精度。
d1=Double.parseDouble(t1.toString());
d2=Double.parseDouble(t2.toString());
你可能想要
d1 = Double.parseDouble(t1.getText().toString());
哦,你的 while 循环看起来很奇怪。你可能需要一个 if 检查,如果文本无效则返回
【讨论】:
以上是关于应用程序在 Android 模拟器中停止工作的主要内容,如果未能解决你的问题,请参考以下文章