如何在Textview中显示计算值

时间:2014-10-06 17:45:48

标签: java android xml textview

我想知道为什么public void onClick(View v)方法中的代码片段无效。当我单击按钮进行计算时,弹出框显示并通知我:应用程序意外停止..但是当我删除那些代码时没有这样的错误(在方法内部公共无效onClick(View v)意味着问题出在该代码上并且不在清单文件上。感谢您回答我的问题和Godbless:D

  import android.app.Activity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.*;

   public class Form2 extends Activity 
   {

    String str1="";
    String str2="";
    String str3="";
    String str4="";
    String str5=""; 
    String str6="";
    String totalString = null;
    double num1=0;
    double num2=0;
    double num3=0;
    double num4=0;
    double num5=0;
    double num6=0;
    double a=0;
    double b=0;
    double c=0;
    double total=0;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.form2);

      final  TextView nameTV=(TextView)findViewById(R.id.nameTV);
        nameTV.setText(getIntent().getExtras().getString("nameKey"));

        final TextView subjTV=(TextView)findViewById(R.id.subjTV);
        subjTV.setText(getIntent().getExtras().getString("subjectKey"));

        final TextView tv1=(TextView)findViewById(R.id.tv1);

        final EditText firstETxt=(EditText)findViewById(R.id.et1);

        final EditText firstAETxt=(EditText)findViewById(R.id.et1a);

        final EditText secondETxt=(EditText)findViewById(R.id.et2);

        final EditText secondAETxt=(EditText)findViewById(R.id.et2a);

        final EditText thirdETxt=(EditText)findViewById(R.id.et3);

        final EditText thirdAETxt=(EditText)findViewById(R.id.et3a);



        ImageButton calcuButt=(ImageButton)findViewById(R.id.calcB);

        calcuButt.setOnClickListener(new View.OnClickListener()
        {
            public void onClick(View v)
            {
                if(v.getId()==R.id.calcB)
                {
                    str1=firstETxt.getText().toString();
                    num1=Double.parseDouble(str1);

                    str2=firstAETxt.getText().toString();
                    num2=Double.parseDouble(str2);

                    str3=secondETxt.getText().toString();
                    num3=Double.parseDouble(str3);

                    str4=secondAETxt.getText().toString();
                    num4=Double.parseDouble(str4);

                    str5=thirdETxt.getText().toString();
                    num5=Double.parseDouble(str5);

                    str6=thirdAETxt.getText().toString();
                    num6=Double.parseDouble(str6);

                    a=num1*num2;
                    b=num3*num4;
                    c=num5*num6;

                    total=a+b+c;

                    totalString = Double.toString(total);

                    tv1.setText(totalString);

            }}


        });

    }
}

这是xml代码:

    <?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout
    android:id="@+id/widget0"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:background="@drawable/bg"
    >
    <TextView
    android:id="@+id/widget33"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter all the scores you already have and"
    android:textSize="16sp"
    android:typeface="sans"
    android:textColor="#000000"
    android:layout_x="10px"
    android:layout_y="42px"
    >
    </TextView>
    <EditText
    android:id="@+id/et3"
    android:layout_width="57px"
    android:layout_height="35px"
    android:textSize="18sp"
    android:layout_x="10px"
    android:layout_y="232px"
    >
    </EditText>
    <EditText
    android:id="@+id/et1"
    android:layout_width="56px"
    android:layout_height="33px"
    android:textSize="13sp"
    android:layout_x="10px"
    android:layout_y="152px"
    >
    </EditText>
    <EditText
    android:id="@+id/et2"
    android:layout_width="55px"
    android:layout_height="34px"
    android:textSize="18sp"
    android:layout_x="10px"
    android:layout_y="192px" 
    >
    </EditText>
    <EditText
    android:id="@+id/et3a"
    android:layout_width="57px"
    android:layout_height="34px"
    android:textSize="18sp"
    android:layout_x="70px"
    android:layout_y="232px"
    >
    </EditText>
    <EditText
    android:id="@+id/et2a"
    android:layout_width="57px"
    android:layout_height="32px"
    android:textSize="18sp"
    android:layout_x="70px"
    android:layout_y="192px"
    >
    </EditText>
    <EditText
    android:id="@+id/et1a"
    android:layout_width="57px"
    android:layout_height="35px"
    android:textSize="13sp"
    android:layout_x="70px"
    android:layout_y="152px"
   >
   </EditText>

    <TextView
    android:id="@+id/tv1"
    android:layout_width="122px"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="#ff000000"
    android:layout_x="120px"
    android:layout_y="162px"
    >
    </TextView>
    <TextView
    android:id="@+id/tv2"
    android:layout_width="128px"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:textColor="#ff000000"
    android:layout_x="120px"
    android:layout_y="212px"
    >
    </TextView>
    <EditText
    android:id="@+id/ete"
    android:layout_width="98px"
    android:layout_height="37px"
    android:layout_x="120px"
    android:layout_y="242px"
    >
    </EditText>
    <TextView 
    android:layout_height="wrap_content"
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:text="SubjectTextView" 
    android:id="@+id/nameTV" 
    android:layout_width="wrap_content" 
    android:textStyle="bold" 
    android:layout_x="13dip" 
    android:layout_y="122dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:text="the equivalent percentage of it in your subject" 
    android:typeface="sans" 
    android:id="@+id/widget34" 
    android:layout_width="wrap_content" 
    android:layout_x="13dip" 
    android:layout_y="74dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="18sp" 
    android:text="nameTextView" 
    android:typeface="sans" 
    android:id="@+id/subjTV" 
    android:layout_width="144px" 
    android:textStyle="bold" 
    android:layout_x="48dip" 
    android:layout_y="32dip">
    </TextView>
    <TextView android:layout_height="wrap_content" 
    android:textColor="#000000" 
    android:textSize="16sp" 
    android:text="Hi" 
    android:typeface="sans" 
    android:id="@+id/widget30" 
    android:layout_width="wrap_content" 
    android:layout_x="22dip" 
    android:layout_y="34dip">
    </TextView>
    <ImageButton android:id="@+id/calcB" 
    android:layout_height="36px" 
    android:layout_width="100px" 
    android:src="@drawable/calc" 
    android:layout_x="122dip" 
    android:layout_y="329dip">
    </ImageButton>
    </AbsoluteLayout>

2 个答案:

答案 0 :(得分:0)

如果没有看到完整的代码,原因可能是以下之一:

  • 如果字符串不包含可解析的double,则Double.parseDouble(strX)调用可以抛出NumberFormatException。这意味着您需要确保它们都包含有效值。
  • 您的一个观点为空,即尚未使用secondETxt = (EditText) findViewById(R.id.YOURID);
  • 之类的调用进行初始化

我的猜测是您的EditText次观看为空,导致上述异常。

答案 1 :(得分:0)

我运行了你的代码,似乎有很多问题。我不会给你一些建议,比如“避免绝对布局”或者你将来会学到的任何东西。

所以,第一次崩溃发生在这行代码

nameTV.setText(getIntent().getExtras().getString("nameKey"));

因此,如果您处于主要活动的上下文中,则无需从Intent类中获取任何内容。结果将始终相同:崩溃 因此,首先要定义您的价值来自何处。

其次,在解析之前必须检查editTexts中的值。它们不必为null,因为您将获得NullPointerException并且值必须至少为数字以避免NumberFormatException。

最后,代码行

 if(v.getId()==R.id.calcB)

没有必要。