2016-05-14 64 views
-3

嘿家伙,我真的很想计算不仅c =斜边,而且还有3或3个EditText-Views中给出的a或b。但它崩溃,不会输出结果。有什么建议么?我对这个东西很新。 :)在android应用程序中的毕达哥拉斯

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/unwanted-myapp-has-stopped-how-can-i-solve-this – CommonsWare

+0

也发布logcat。 –

回答

4

编辑 有趣的是没有一个较早指出了这一点,但我只是意识到你永远不会初始化cNumPyt 。您初始化其他EditText s但从来没有C.因此,当您尝试获取文本时,您获得NullPointerException,因为您尝试从无所得到的信息。

onCreate方法中初始化正确的文本框。


在你的形象,你表明你离开输入空白的一个文本框(在“后崩溃”的形象,我们看到,你进入3,4,再没有什么)。这意味着没有Double被解析,因为""不是一个数字。

Double documentation

public static double parseDouble(String s) throws NumberFormatException
[...]
抛出:
NullPointerException - 如果字符串为null
NumberFormatException - 如果字符串不包含可解析的两倍。

如果你的输入是空白的,你会得到一个异常。由于它不在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

嗯,这是有道理的,我试图用你的示例打印什么字母丢失,而不是崩溃..但它仍然不会工作 尝试a = Double.parseDouble(aNumPyt.getText()。toString()) ; } catch(NumberFormatException e){oops,没有可用的数字 a = 0; //默认为0(或任何你选择的) pythagorasResultFinal.setText(“No A input”); } 顺便说一句:感谢您的真正快速反应 –

+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中,因为Arc676解释了...即使我在空输入中输入0,它仍然崩溃领域。 :/ –

+0

你把android:inputType =“numberDecimal”放在你的xml文件中吗? –

0

我发现问题.. 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)); 
    } 
} 

我发现另一个重要的问题是我没有定义ID为C查看..真是小问题造成这一切崩溃..

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

感谢您的快速响应和帮助! (y)

相关问题