问题描述
应用程序崩溃,出现以下错误:
致命异常:主进程:com.example.ayyan.jellybeanestimator,PID:2960 java.lang.RuntimeException:无法启动活动
ComponentInfo {com.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:空字符串在com.example.ayyan.jell上的java.lang.Double.parseDouble(Double.java:547)上的java.lang.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1071) ybeanestimator.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 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)上的.lang.reflect.Method.invoke(本机方法),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);
}
}
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:空字符串。
4楼
您可以尝试更改:
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));
这应该将双精度型转换为字符串。
5楼
@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();
}
}