我正在使用按钮从一个活动切换到另一个活动。在同一活动中,浮动操作按钮运行良好,但正常按钮导致异常。我提供按钮相关的java类,xml文件和logcat。
IndexPage.java
public class IndexPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent in = new Intent(IndexPage.this, FoodEntry.class);
startActivity(in);
}
});
Button b = (Button)findViewById(R.id.buttonBackUp);
b.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(IndexPage.this, Form.class);
startActivity(intent);
}
});
}
}
activity_index_page.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.praval.healthfreak.IndexPage">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="@layout/content_index_page" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/newfloatnew" />
</android.support.design.widget.CoordinatorLayout>
content_index_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.praval.healthfreak.IndexPage"
tools:showIn="@layout/activity_index_page">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/buttonBackUp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
logcat
04-02 17:54:06.569 24689-24689/com.praval.healthfreak W/dalvikvm: threadid=1: calling UncaughtExceptionHandler
04-02 17:54:06.577 24689-24689/com.praval.healthfreak E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.praval.healthfreak, PID: 24689
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.praval.healthfreak/com.praval.healthfreak.Form}: java.lang.NumberFormatException: Invalid int: ""
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2389)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NumberFormatException: Invalid int: ""
at java.lang.Integer.invalidInt(Integer.java:137)
at java.lang.Integer.parseInt(Integer.java:358)
at java.lang.Integer.parseInt(Integer.java:331)
at com.praval.healthfreak.Form.onCreate(Form.java:30)
at android.app.Activity.performCreate(Activity.java:5343)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1088)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2441)
at android.app.ActivityThread.access$900(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:5345)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
at dalvik.system.NativeStart.main(Native Method)
(编辑)---&gt; (新版本根据你的所有建议,但我仍然被卡住了)
Form.java
public class Form extends AppCompatActivity {
String g="m";
int a=0,w=0,f=0,i=0;
double val , bmi;
int cal=0;
TextView tBmi = (TextView)findViewById(R.id.textResult);
TextView tFitOrNot = (TextView)findViewById(R.id.textFitOrNot);
TextView tCalorie = (TextView)findViewById(R.id.textCalorie);
EditText gender = (EditText)findViewById(R.id.editGender);
EditText age = (EditText)findViewById(R.id.editAge);
EditText weight = (EditText)findViewById(R.id.editWeight);
EditText feet = (EditText)findViewById(R.id.editFeet);
EditText inch = (EditText)findViewById(R.id.editInch);
static String s1="underweight";
static String s2="fit";
static String s3="overweight";
static String s4="obese";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Button bCalculate = (Button)findViewById(R.id.buttonBMI);
bCalculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
calculation();
}
});
}
public void calculation() {
/* gender.setText("m");
age.setText("m");
weight.setText("m");
feet.setText("m");
inch.setText("m"); */
g = gender.getText().toString();
a = Integer.parseInt(age.getText().toString());
w = Integer.parseInt(weight.getText().toString());
f = Integer.parseInt(feet.getText().toString());
i = Integer.parseInt(inch.getText().toString());
//feet and inches converted to metre
val=((f*12)+i)*0.245;
bmi=w*100/(val*val);
tBmi.setText(String.valueOf(bmi));
if (bmi<18.5)
{
tFitOrNot.setText(s1);
if(g.equals("m"))
{
if(a>=18 && a<=25)
{
cal=3200;
// tCalorie.setText(s5);
}
if(a>25 && a<=40)
{
cal=3400;
//tCalorie.setText(s6);
}
if(a>40 && a<=60)
{
cal=3400;
// tCalorie.setText(s6);
}
}
else
{
if(a>=18 && a<=25)
{
cal=3100;
//tCalorie.setText(s7);
}
if(a>25 && a<=40)
{
cal=2900;
//tCalorie.setText(s8);
}
if(a>40 && a<=60)
{
cal=2900;
// tCalorie.setText(s8);
}
}
}
else if(bmi>=18.5 && bmi<=24.9)
{
tFitOrNot.setText(s2);
if(g.equals("m"))
{
if(a>=18 && a<=25)
{
cal=3000;
// tCalorie.setText(s9);
}
if(a>25 && a<=40)
{
cal=2800;
// tCalorie.setText(s12);
}
if(a>40 && a<=60)
{
cal=2700;
// tCalorie.setText(s10);
}
}
else
{
if(a>=18 && a<=25)
{
cal=2700;
//tCalorie.setText(s10);
}
if(a>25 && a<=40)
{
cal=2400;
//tCalorie.setText(s14);
}
if(a>40 && a<=60)
{
cal=2000;
//tCalorie.setText(s13);
}
}
}
else if(bmi>=25 && bmi<=29.9)
{
tFitOrNot.setText(s3);
if(g.equals("m"))
{
if(a>=18 && a<=25)
{
cal=2200;
//tCalorie.setText(s11);
}
if(a>25 && a<=40)
{
cal=2200;
// tCalorie.setText(s11);
}
if(a>40 && a<=60)
{
cal=1200;
//tCalorie.setText(s16);
}
}
else
{
if(a>=18 && a<=25)
{
cal=1200;
//tCalorie.setText(s16);
}
if(a>25 && a<=40)
{
cal=1500;
// tCalorie.setText(s15);
}
if(a>40 && a<=60)
{
cal= 1300;
// tCalorie.setText(s19);
}
}
}
else
{
tFitOrNot.setText(s4);
if(g.equals("m"))
{
if(a>=18 && a<=25)
{
cal=2000;
//tCalorie.setText(s13);
}
if(a>25 && a<=40)
{
cal=2500;
//tCalorie.setText(s18);
}
if(a>40 && a<=60)
{
cal=2050;
//tCalorie.setText(s17);
}
}
else
{
if(a>=18 && a<=25)
{
cal=1000;
//tCalorie.setText(s20);
}
if(a>25 && a<=40)
{
cal=1300;
//tCalorie.setText(s19);
}
if(a>40 && a<=60)
{
cal=1200;
//tCalorie.setText(s16);
}
}
}
tCalorie.setText(String.valueOf(cal));
}
}
答案 0 :(得分:1)
您正在通过""
获取Integer
个对象。这导致exception
。
下面:
a = Integer.parseInt(age.getText().toString());
w = Integer.parseInt(weight.getText().toString());
f = Integer.parseInt(feet.getText().toString());
i = Integer.parseInt(inch.getText().toString());
将您的计算结果排除在onCreate
之外,因为如果您未向EditText
设置任何内容,则会始终抛出exception
。例如,您可以将这些按钮单击。
尝试将解析放在try
catch
块中以防止Run time exceptions
。
例如:
try{
a = Integer.parseInt(age.getText().toString());
// similarly others.
}catch(NumberFormatException e){
}
答案 1 :(得分:1)
a = Integer.parseInt(age.getText().toString());
w = Integer.parseInt(weight.getText().toString());
f = Integer.parseInt(feet.getText().toString());
i = Integer.parseInt(inch.getText().toString());
将这些线条移动到按钮单击或其他内容中。因为当您的活动运行时,您的edittexts为空并返回&#34; &#34;当你写age.getText().toString()
时。那&#34; &#34;被传递给生成错误的Integer.parseInt()
函数。在调用Integer.parseInt()
函数
答案 2 :(得分:0)
当您启动表单活动时,您的应用程序崩溃,因为您的所有EditTexts都为空,而您的代码实际上会在此处引发错误
Integer.parseInt("");
要解决此问题,您可能应该向表单布局XML添加一个按钮,并将计算值的大部分代码移动到onClick方法中。
我还建议将逻辑分解为某些方法,而不是将所有内容都放在onCreate中,因为那些if语句看起来像很多重复的代码