2016-05-14 2 views
-3

ちょっと私は3つのEditText-Viewsで与えられたものからc =斜辺だけでなくaまたはbを計算したいと思っています。しかし、それはクラッシュし、結果を出力しません。助言がありますか?私はこのことにかなり新しいです。 :)アンドロイドアプリケーションのPythagoras

package net.starostka.somefunctions; 

import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.text.TextUtils; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class Pythagoras extends AppCompatActivity implements View.OnClickListener { 

EditText aNumPyt; 
EditText bNumPyt; 
EditText cNumPyt; 

Button pythagorasCalcu; 

TextView pythagorasResultFinal; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pythagoras); 

    aNumPyt = (EditText) findViewById(R.id.aNumPyt); 
    bNumPyt = (EditText) findViewById(R.id.bNumPyt); 

    pythagorasCalcu = (Button) findViewById(R.id.pythagorasCalcu); 

    pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal); 

    // set a listener 
    pythagorasCalcu.setOnClickListener(this); 
} 

@Override 
public void onClick(View v) { 
    double a=0,b=0,c=0; 
    double hypot = 0.0; 

    /* 
    // check if the fields are empty 
    if (TextUtils.isEmpty(aNumPyt.getText().toString()) || TextUtils.isEmpty(bNumPyt.getText().toString()) || TextUtils.isEmpty(cNumPyt.getText().toString())) { 
     return; 
    } 
    */ 

    // read EditText and fill variables with numbers 
    a = Double.parseDouble(aNumPyt.getText().toString()); 
    b = Double.parseDouble(bNumPyt.getText().toString()); 
    c = Double.parseDouble(cNumPyt.getText().toString()); 

    if (a>b && a>c){ 
     hypot=a; 
     if(hypot*hypot==(b*b+c*c)){ 
      //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2)); 
      pythagorasResultFinal.setText(String.valueOf(hypot)); 
     } 
    } else if (b>a && b>c){ 
     hypot=b; 
     if(hypot*hypot==(a*a+c*c)){ 
      //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2)); 
      pythagorasResultFinal.setText(String.valueOf(hypot)); 
     } 
    } else if (c>a && c>b){ 
     hypot=c; 
     if(hypot*hypot==(a*a+b*b)){ 
      //result = Math.sqrt(Math.pow(a,2)+Math.pow(b,2)); 
      pythagorasResultFinal.setText(String.valueOf(hypot)); 
     } 
    } 
} 

}

XMLファイル

</LinearLayout> 
    <TextView 
     android:layout_height="wrap_content" 
     android:layout_width="match_parent" 
     android:layout_marginLeft="5pt" 
     android:layout_marginRight="5pt" 
     android:textSize="12pt" 
     android:layout_marginTop="3pt" 
     android:id="@+id/pythagorasResultFinal" 
     android:gravity="center_horizontal"> 
    </TextView> 

クラッシュする前に:

クラッシュ後:

+2

LogCatを使用して、クラッシュに関連するJavaスタックトレースを調べます。https://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

また、 logcatを投稿してください。 –

答えて

4

EDIT誰が先にこれを指摘していないが、私はちょうどあなたがcNumPytを初期化することはありません実現ことは興味深い 。他のEditTextを初期化しますが、決してCを入力しないでください。テキストを取得しようとすると、何の情報も得られないため、NullPointerExceptionになります。

onCreateメソッドで正しいテキストボックスを初期化します。あなたのイメージで


は、あなたがテキストボックスのいずれかの空白の入力を残していることを示している(「後にクラッシュ」の画像では、我々はあなたが3、4、その後、何も入力されていないことを確認します)。つまり、""は数字ではないため、解析する数字はDoubleではありません。 Double documentationから

public static double parseDouble(String s) throws NumberFormatException
[...]
例外:
NullPointerExceptionを - 文字列が解析可能な二重が含まれていない場合 - 文字列がNumberFormatException
nullの場合。

入力が空白の場合は例外が発生します。 try-catchブロックにないので、アプリケーションがクラッシュします。

は、具体的には:

//cNumPyt is empty, and "" is not a parseable number, so NumberFormatException 
c = Double.parseDouble(cNumPyt.getText().toString()); 

サンプル・ブロックは、(あなたが空だった変数伝えることができるので、あなたは別に、すべての変数のためにこれを行う必要があります):

try{ 
    a = Double.parseDouble(aNumPyt.getText().toString()); 
}catch (NumberFormatException e){ 
    //oops, no number available 
    a = 0; //default to 0 (or whatever you choose) 
} 
+0

うーん、それは意味がある、私はあなたのサンプルを使用して、どの文字が欠落していたのかが分かりませんでした..しかし、それでも動作しませんでした。 ; } catch(NumberFormatException e){ // oops、利用可能な番号がありません a = 0; //デフォルト値は0(または選択したもの) pythagorasResultFinal.setText( "No A input"); } Btw:本当に速い応答に感謝します –

+0

@BenjaminStarostkaJakobsen "動作しません"より具体的なものはありますか?それはまだクラッシュするように? 'a'、' b'、_and_'c'に別々の 'try'ブロックを使いましたか? – Arc676

+0

もちろん、申し訳ありません。それはまだ私の意味だったクラッシュします。 ここに図があります:http://imgur.com/AGOF7Yf –

2
@Override 
public void onClick(View v) { 
double a=0,b=0,c=0; 


if (!TextUtils.isEmpty(aNumPyt.getText().toString()) && 
    !TextUtils.isEmpty(bNumPyt.getText().toString()) && 
    !TextUtils.isEmpty(cNumPyt.getText().toString())) 

{ 

a = Double.parseDouble(aNumPyt.getText().toString()); 
b = Double.parseDouble(bNumPyt.getText().toString()); 
c = Double.parseDouble(cNumPyt.getText().toString()); 


    if((a*a)==((b*b)+(c*c))){ 
     pythagorasResultFinal.setText(String.valueOf(a)); 
    } 
    else if ((b*b)==((a*a)+(c*c))){ 
     pythagorasResultFinal.setText(String.valueOf(b)); 
    } 
    else if ((c*c)==((a*a)+(b*b))){ 
     pythagorasResultFinal.setText(String.valueOf(c)); 
    } 
} 

} 
+0

私はあなたの解決策を試しましたが、問題はまだ説明されているようにNumberFormatExceptionに置かれているようです...空の入力に0を入れてもクラッシュしますフィールド。 :/ –

+0

あなたのXMLファイルにandroid:inputType = "numberDecimal"を入れましたか? –

0

I問題を発見しました。Arc676のソリューションは、以下のコードのように機能しました:

@Override 
public void onClick(View v) { 
    double a=0.0,b=0.0,c=0.0; 
    double hypot = 0.0; 

    try{ 
     a = Double.parseDouble(aNumPyt.getText().toString()); 
    }catch (NumberFormatException e){ 
     //oops, no number available 
     a = 0.0; //default to 0 (or whatever you choose) 
    } 
    try{ 
     b = Double.parseDouble(bNumPyt.getText().toString()); 
    }catch (NumberFormatException e){ 
     //oops, no number available 
     b = 0.0; //default to 0 (or whatever you choose) 
    } 
    try{ 
     c = Double.parseDouble(cNumPyt.getText().toString()); 
    }catch (NumberFormatException e){ 
     //oops, no number available 
     c = 0.0; //default to 0 (or whatever you choose) 
    } 

    if (c == 0.0){ 
     hypot = Math.sqrt((Math.pow(a,2))+(Math.pow(b,2))); 
     pythagorasResultFinal.setText("Finding C\n" + String.valueOf(hypot)); 
    }else if (a == 0.0){ 
     hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(b,2))); 
     pythagorasResultFinal.setText("Finding A\n" + String.valueOf(hypot)); 
    }else if (b == 0.0) { 
     hypot = Math.sqrt((Math.pow(c,2))-(Math.pow(a,2))); 
     pythagorasResultFinal.setText("Finding B\n" + String.valueOf(hypot)); 
    } 
} 
01私が見つけた

もう一つの本質的な問題は、私はそれがすべて...高速応答と助けを

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_pythagoras); 

    aNumPyt = (EditText) findViewById(R.id.aNumPyt); 
    bNumPyt = (EditText) findViewById(R.id.bNumPyt); 
    cNumPyt = (EditText) findViewById(R.id.cNumPyt); //Forgot to add this line of code 

    pythagorasCalcu = (Button) findViewById(R.id.pythagorasCalcu); 

    pythagorasResultFinal = (TextView) findViewById(R.id.pythagorasResultFinal); 

    // set a listener 
    pythagorasCalcu.setOnClickListener(this); 
} 

感謝をクラッシュさせる.. Cの表示のために本当に小さな問題をIDを定義していませんでした! (y)

関連する問題