为啥我的应用程序崩溃而没有任何错误?
Posted
技术标签:
【中文标题】为啥我的应用程序崩溃而没有任何错误?【英文标题】:Why does my app crash without any errors?为什么我的应用程序崩溃而没有任何错误? 【发布时间】:2017-06-28 08:18:40 【问题描述】:应用程序崩溃,我收到以下错误:
致命异常:主要 进程:com.example.ayyan.jellybeanestimator,PID:2960 java.lang.RuntimeException:无法启动活动
ComponentInfocom.example.ayyan.jellybeanestimator/com.example.ayyan.jellybeanestimator.MainActivity: java.lang.NumberFormatException:空字符串 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 引起:java.lang.NumberFormatException:空字符串 在 java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) 在 java.lang.Double.parseDouble(Double.java:547) 在 com.example.ayyan.jellybeanestimator.MainActivity.onCreate(MainActivity.java:25) 在 android.app.Activity.performCreate(Activity.java:6662) 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118) 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6077) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
package com.example.ayyan.jellybeanestimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.EditText;
import android.widget.Button;
import android.content.Intent;
import android.view.View;
public class MainActivity extends AppCompatActivity
EditText jellyBeanLength, jellyBeanDiameter, jarSizeVolume;
double jellybeantall, jellybeanfat, jellybeanspace;
double loadFactor = .698;
TextView answer;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
startActivity(new Intent(MainActivity.this, MainActivity.class));
);
double volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
double volumeofBeans = (jellybeanspace*loadFactor);
float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
answer.setText("You have this amount of beans in your jar" + numberOfBeans);
【问题讨论】:
"应用程序崩溃但没有错误。出现这些错误:" ??? 它有错误。它们只是不是编译时错误。不是所有的都是。您的问题是: 引起:java.lang.NumberFormatException: empty String 您正在将空字符串转换为双精度,这是不允许的。所以首先检查它是否为空,而不是解析后:) 崩溃是由NumberFormatException
引起的,其背后的原因是您的编辑文本的一个(或多个)文本是空的。在将编辑文本的文本转换为双精度文本之前,请检查文本是否为空以及它们是否为有效数字。
startActivity(new Intent(MainActivity.this, MainActivity.class));为什么你调用同一个活动
【参考方案1】:
检查您是否在EditText
中插入了任何值,然后仅在Double
中解析它
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
//Take out value when button pressed
jellybeantall = Double.parseDouble(TextUtils.isEmpty(jellyBeanLength.getText().toString())?"0":jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(TextUtils.isEmpty(jellyBeanDiameter.getText().toString())?"0":jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(TextUtils.isEmpty(jarSizeVolume.getText().toString())?"0":jarSizeVolume.getText().toString());
double volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
double volumeofBeans = (jellybeanspace*loadFactor);
float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
answer.setText("You have this amount of beans in your jar" + numberOfBeans);
);
【讨论】:
【参考方案2】:您应该计算按钮的 onClick。在您的代码中,您没有输入任何值并尝试计算它。
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
double volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
double volumeofBeans = (jellybeanspace*loadFactor);
float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
answer.setText("You have this amount of beans in your jar" + numberOfBeans);
);
【讨论】:
【参考方案3】:EditText 字段中没有值,您正在尝试解析该值。因此出现错误 java.lang.NumberFormatException: empty String。
【讨论】:
【参考方案4】:@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try
jellyBeanLength = (EditText) findViewById (R.id.length);
jellyBeanDiameter = (EditText) findViewById (R.id.diameter);
jarSizeVolume = (EditText) findViewById (R.id.jarsize);
jellybeantall = Double.parseDouble(jellyBeanLength.getText().toString());
jellybeanfat = Double.parseDouble(jellyBeanDiameter.getText().toString());
jellybeanspace = Double.parseDouble(jarSizeVolume.getText().toString());
answer = (TextView) findViewById (R.id.answer);
Button calculate = (Button) findViewById (R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
double volumeOfOneJellyBean = ((3.14159265359/6)*(jellybeanfat*jellybeanfat)*jellybeantall);
double volumeofBeans = (jellybeanspace*loadFactor);
float numberOfBeans = (float)Math.round (volumeofBeans/volumeOfOneJellyBean);
answer.setText("You have this amount of beans in your jar" + numberOfBeans);
);
catch (Exception e)
e.printStackTrace();
【讨论】:
请为您的回答提供更多背景信息。代码本身不是答案 尝试捕获不是解决方案。【参考方案5】:你可以尝试改变:
answer.setText("You have this amount of beans in your jar" + numberOfBeans);
到
answer.setText("You have this amount of beans in your jar" + String.valueOf(numberOfBeans));
这应该将双精度转换为字符串。
【讨论】:
附加字符串的整数值被视为最终字符串。因此,当您已经将整数附加到字符串时,没有必要将整数包装在字符串中。以上是关于为啥我的应用程序崩溃而没有任何错误?的主要内容,如果未能解决你的问题,请参考以下文章