2016-04-02 142 views
-2

我正在使用按钮从一个活动切换到另一个活动。在相同的活动浮动动作按钮运作良好,但正常的按钮导致异常。我正在提供按钮相关的java类,xml文件和logcat。Android应用程序在点击按钮时崩溃

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)  

(EDIT)--->(根据新版本的所有建议,但还是我坚持)

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)); 
} 
} 

编辑 - 在EDITTEXT Screenshot

+0

的错误是在你的Form.java类。发布该课程,请 –

+1

您正在使用正常按钮调用具有浮动按钮和“Form”活动的“FoodEntry”活动。错误显然是在你的“表格”活动中 –

+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。例如,您可以将这些放在按钮上单击。

并尝试在trycatch块内部进行解析以防止Run time exceptions

例:

try{ 
    a = Integer.parseInt(age.getText().toString()); 
    // similarly others. 
}catch(NumberFormatException e){ 

} 
+1

需要impove ... –

+0

只需编辑... –

+0

@RohitArya我已经编辑Form.java但还是我坚持 –

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()); 

移动这些按钮点击或里面的东西线。因为当你的活动运行时,你的编辑文本是空的,并且当你写入age.getText().toString()时返回“”。该“”被传递给产生错误的函数Integer.parseInt()。并确保您的edittexts不是空的调用Integer.parseInt()功能

+0

注意:“”表示edittexts为空 –

+0

它是一种表单,所以我不能使用按钮单击。我会尽力处理你的建议并尽快回复你。 –

+0

如何在调用parseInt()之前给所有EditTexts赋值? –

0

您的应用崩溃之前,当您启动形式的活动,因为所有的EditTexts是空的,你的代码基本上会在这

Integer.parseInt(""); 

抛出一个错误修复这个问题,你可能应该向Form布局XML添加一个按钮,并将计算你的值的大部分代码移动到onClick方法中。

我还建议打破逻辑到一些方法,而不是因为那些坚持到一切的onCreate if语句看起来好像有很多重复的代码

+0

并请告诉我如何调用的Integer.parseInt之前给数值全部EditTexts(); –

+0

你键入它们,然后单击按钮 –

+0

我已经编辑Form.java但还是我坚持 –

相关问题