2016-04-02 9 views
-2

ボタンを使用して1つのアクティビティから別のアクティビティに切り替わります。同じアクティビティフローティングアクションボタンでは正常に機能していますが、通常のボタンでは例外が発生しています。私はボタン関連のJavaクラス、XMLファイル、ログキャットを提供しています。ボタンをクリックすると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)); 
} 
} 

EDIT - >入力された値EDITTEXT でScreenshot

+0

ように見える場合は、エラーがあなたのForm.javaクラスにあるいくつかのメソッドにロジックを壊すのではなく、それら以来のonCreateにすべてを貼り付けることをお勧めします。そのクラスを投稿してください。 –

+1

フローティングボタンで 'FoodEntry'アクティビティを呼び出し、通常ボタンで' Form'アクティビティを呼び出しています。あなたの 'Form'アクティビティに明らかにエラーがあります –

+0

' Form'クラスを追加してください。エラーは 'activity'にあります。 –

答えて

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

あなたはEditTextには何も設定していない場合、それは常にexceptionをスローしますのでonCreateのあなたの計算を入れてください。たとえば、ボタンをクリックするとこれらのボタンを置くことができます。

Run time exceptionsを防ぐために、内部に構文解析を入れてみてください。trycatchブロック

例:ボタンクリックまたは何か内部

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

} 
+1

... –

+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

注: ""はedittextが空であることを意味します –

+0

ボタンクリックを使用できないフォームです。私はあなたの提案に取り組み、できるだけ早く対応しようとします。 –

+0

parseInt()を呼び出す前にすべてのEditTextに値を与える方法? –

0

アプリがクラッシュするあなたのedittextsはInteger.parseInt()関数を呼び出す前に空でないことを確認してくださいフォームレイアウトXMLにボタンを追加し、値を計算するコードの大部分をonClickメソッドに移動する必要があるかもしれません。

私はまた、文が重複したコードの多く

+0

ボタンクリックですべての計算を移動し、Integer.ParseInt()を呼び出す前にすべてのEditTextに値を与える方法を教えてください。 –

+0

それらを入力してボタンをクリックします –

+0

Form.javaを編集しましたが、まだスタックしています –

関連する問題